Set a custom template variable in expressionengine

onblur picture onblur · Sep 26, 2011 · Viewed 12k times · Source

I need to output the category for an entry a few times in an entry's template.

So I want to get the output from the following and assign to a variable so I can reuse within the template:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}{category_name}{/categories}
{/exp:channel:entries}"

How do it do that?

Answer

Marcus Olsson picture Marcus Olsson · Oct 10, 2011

Now, you could enable the template to allow PHP, then you could write something like this:

{exp:channel:entries channel="product" limit="1" status="open"}
    {categories}
        <?php $category = '{category_name}'; ?>
    {/categories}
{/exp:channel:entries}

Then you have the {category_name} stored in the php-variable "category". Later you can reuse it as you wish, like echoing it:

<?php echo $category; ?>

You can even compare it to other EE-tags:

{exp:channel:entries channel="product" limit="1" status="open"}
    {if <?php $echo($category) ?> == title}
        This title have got the same value as the category!
    {/if}
{/exp:channel:entries}