0

I am trying to get the current entry's category so I can make a comparison inside a menu elsewhere to see if the menu item is for the current page. I have the following code at the top of my template:

{exp:channel:entries}
    {categories limit="1"}
        {preload_replace:current_category="{category_id}"}  
        {current_category}                      
    {/categories}
{/exp:channel:entries}

which gives an output of 56

However, if I move {current_category} out of the pair so I can use it elsewhere, it does not evaluate:

{exp:channel:entries}
    {categories limit="1"}
        {preload_replace:current_category="{category_id}"}  
    {/categories}
{/exp:channel:entries}
{current_category}

gives an output of {category_id}

How can I get the replace variable to save the evaluated category_id inside of the pair so I can use it elsewhere? Or, is there a better way to be doing this?

2

There's a better way, though I'm not sure what where you're trying to insert the entry's category_id.

Use Stash, you'll be glad you did.

{exp:channel:entries}
  {exp:stash:set name="category_id"}
    {categories limit="1"}
      {category_id}                  
    {/categories}
  {/exp:stash:set}
{/exp:channel:entries} 

<!-- Then get it after entries loop -->
{exp:stash:get name="category_id"}
| improve this answer | |
0

You can do this using an embed.

{embed="group/.template" current_category="{exp:channel:entries}{categories limit="1"}{category_id}{/categories}{/exp:channel:entries}" }

On the embed template, just call {embed:current_category}.

I think it's better than install stash for use just once.

| improve this answer | |
  • I like this as well but now that I've gotten the hang of Stash, I'm using it all over the place! :) – Michael May 21 '13 at 21:39
  • 1
    I have thinking that stash is really overused. Other day I saw a really so amazing complex template all built in the power of Stash. Sometimes it can be great, but too much power can be dangerous. I have made this mistake, too, sometime ago. After some time, my template was too complex to keep working on it. Now, I prefer use addons just when I really need then. Have a nice day! – Robson Sobral May 21 '13 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.