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.

Help Creating Dynamic Navigation "current" Class

April 27, 2011 6:14pm

Subscribe [3]
  • #1 / Apr 27, 2011 6:14pm

    Unifusion

    103 posts

    Hello,

    So I have a dynamic sidebar navigation with a certain channel’s entries, and I’m trying to figure out how to add class=‘current’ to the current page’s navigation item.

    Here’s what I have:

    <ul>
         {exp:channel:entries channel="products" dynamic="off" orderby="title" sort="asc"}
                <li}><a href="http://{path=products/{url_title}}">{title}</a></li>
         {/exp:channel:entries}
         <ul>

    I thought I could add a class for the current page by using the {url_title} tag, like this:

    <li {if segment_2 == '{url_title}'}class="current"{/if}><a href="http://{path=products/{url_title}}">{title}</a></li>

    But that didn’t work, I assume because the URL Title is parsed after the if statement.

    Can you suggest a way of making this work? Thanks!

  • #2 / Apr 28, 2011 6:39am

    John Henry Donovan

    12339 posts

    Unifusion,

    Note that it is dynamic=“no” and the URL title usually resides on segment_3 unless you are rolling your own URLs


    Try this

    <li {if url_title == "{segment_2}"}class="current"{/if}><a href="http://{path=products/{url_title}}">{title}</a></li>
  • #3 / Apr 28, 2011 12:32pm

    Unifusion

    103 posts

    Yup, worked like a charm! If we’ve learned anything from Star Trek, it’s that reversing the polarity always fixes the problem, right? 😊 Thanks, John.

  • #4 / Apr 28, 2011 7:43pm

    Brandon Jones

    5500 posts

    Glad John Henry helped, Unifusion!

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

ExpressionEngine News!

#eecms, #events, #releases