ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Highlighting an item in a category list

October 01, 2010 1:52pm

Subscribe [2]
  • #1 / Oct 01, 2010 1:52pm

    emarthinsen

    4 posts

    Hello-

    I’m outputting a list of categories as part of my site’s header. I want to apply the “active” class to the A tag for the current category that I am on. Here’s my first attempt at the code:

    {exp:channel:categories channel="projects" style="nested" id="project_categories"}
        <a href="http://{path=%27projects/index%27}" class="{if category_id == {exp:channel:category_heading channel=projects}{category_id}{/exp:channel:category_heading}}active{/if}">{category_name}</a>
    {/exp:channel:categories}

    Basically, I’m trying to apply the active class if the category_id in the outputted list matches the category_id returned from the category_heading tag. Unfortunately, this doesn’t work and my hunch is that it is pretty inelegant.

    What is the right way to do this?

    Regards,
    Eric

  • #2 / Oct 02, 2010 10:58am

    John St-Amand

    865 posts

    I’ve tended to do this based on the URL using a segment. So rather than nest a heading tag inside the channel:categories tag, you can just do a lookup of appropriate segment in the URL from within the channel:categories tag pair.  So something like:

    {exp:channel:categories channel="your-channel" category_group="#" style="nested"}
    <a href="http://{path=template_group/listing_template}{if">{category_name}</a>
    {/exp:channel:categories}

    Hope that helps.

  • #3 / Oct 02, 2010 9:57pm

    emarthinsen

    4 posts

    That did the trick. Thanks!

    BTW, here is the final code that I used:

    {exp:channel:categories channel="projects" style="nested" id="project_categories"}
        <a href="http://{path=%27projects/index%27}" class="{if segment_3 == category_url_title}active{/if}">{category_name}</a>
    {/exp:channel:categories}
  • #4 / Oct 05, 2010 5:59am

    John St-Amand

    865 posts

    Great. Glad that helped!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases