0

I want to list out all posts based on the category_id of the current entry. But how can I get the category_id before the loop?

Basically, what I could do is this:

{exp:channel:entries channel"mychannel" category_id="2"}
 {title}
{/exp:channel:entries}

The problem is, I don't know the category_id at that point. As of now, I'm doing a basic channel entries loop, and run an embedded template that I pass the category_id to. And that template lists out all the posts with that category set.

So using gwcode_categories, I can get the category_id and pass that along with the embedded template, like this:

{exp:channel:entries channel="mychannel"}
    {embed="include/get_posts"  
    category="{exp:gwcode_categories entry_id="{entry_id}" last_only="yes" style="linear"}{cat_id}{/exp:gwcode_categories}"}
{/exp:channel:entries}

My categories are nested, so that's why I'm using last_only btw.

But, is there a plugin of some sort that I can use to get the category_id of the current entry before the loop? Like some sort of global dynamic variable?

  • Nevermind, I found this plugin called Low Seg2Cat which is just amazing. Did exactly what I needed, I just used '{last_segment_category_id}' in the initial channel entries loop and that's it! :) I am a happy camper :) – Kenny Bones Feb 19 '14 at 10:13
1

I just found the solution by using this free addon called Low Seg2Cat which just lets me get the category_id of the current uri and pass that along with the initial channel:entries loop. Like this:

{exp:channel:entries channel="mychannel" category="{last_segment_category_id}" dynamic="no"}

I spent hours making that embedded template thing to work. And now I don't even need it. Allthough I did learn that it's possible to accomplish amazing stuff by embedding templates, so I got that going for me, which is nice.

| improve this answer | |

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.