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.

Marking current category as 'class="current"'

March 06, 2012 8:42pm

Subscribe [4]
  • #1 / Mar 06, 2012 8:42pm

    sparky

    17 posts

    The title pretty much says it all 😊

    I have a page that displays my categories, but I want to be able to mark whichever category I’m in as “current”
    The code for the category links I have looks like this:

    {exp:channel:categories channel="resources" style="nested" class="categories"}
     <a href="http://{path=resources}">{category_name}</a>
    {/exp:channel:categories}

    I’ve looked and know I need to do something with segment variable, but I’m not sure quite how to implement this?
    I want to be able to make the a href have a class of “current” if that’s the category that’s currently being viewed.

    Thanks!!

  • #2 / Mar 07, 2012 7:42am

    3Easy

    44 posts

    Depending on the segment of your URL structure relating to categories, the segment variable {segment_n} will give you a slug, which kinda represents Category URL Title. None of the variables from the {exp:channel:categories} tag will match it, not {category_id} and not even {category_name} because there’ll be spaces and differences.

    There’s a treat in store for you today. I’m guessing you haven’t heard about Low’s “Seg2Cat” which is essential to working with categories. Once installed, you get access to information about categories in the URL, in the form of instant variables like {segment_n_category_id}. It’s magic.

    With that, you can do comparisons between the Channel Categories Tag variables and the Seg2Cat variables, and inside of an {if} statement, conditionally apply a class when you get a match.

  • #3 / Mar 07, 2012 8:22am

    Boyink!

    5011 posts

    Hmm…not sure an add-on is needed here.  Try this inside your loop:

    {if segment_3 == category_url_title} class="current" {/if}

    Adjust the segment variable to suit -it needs to match where your category url titles are displaying.

     

     

  • #4 / Mar 07, 2012 9:27am

    sparky

    17 posts

    {if segment_3 == category_url_title} class="current" {/if}

    This worked perfect! Thank you very much, I’m so impressed with these forums every time I have a question! Such quick, and thoughtful, responses. Thank you both for your help 😉

  • #5 / Mar 08, 2012 10:23am

    Shane Eckert

    7174 posts

    Hello sparky,

    I am really glad to see that Mike provided a solution for you. Thanks Mike!

    Seems you have been around about a month on the forums, if you have no been welcomed already, then welcome to the forums!

    Just a side note. Mike has a really great book that I read and in it I learned that cool usage as well as a lot more, especially with navigation and CSS. Worth a read if you are new to ExpressionEngine or have never built out an entire site and want to do it right.

    Cheers,

  • #6 / Mar 08, 2012 10:27am

    sparky

    17 posts

    Thanks for the welcome Shane. The forums have been very helpful! I’ve been working on moving a static site over to EE and it’s gone very smooth so far. I’ve actually been eyeing that ebook you mentioned as well, nice job on the plug btw 😉

  • #7 / Mar 08, 2012 11:08am

    Shane Eckert

    7174 posts

    Hey sparky,

    That’s awesome! I love hearing we are helpful. 😊

    Let me know if you need anything else, you know where to find me.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases