Support » Themes and Templates » keeping current navigation highlighted

  • jinsan

    (@jinsan)


    I think everytime there is a request like this it seems to be answered with a link to sites x, y and z – which offer solutions but not how to implement the solutions.

    I was reading this artcile on A List Apart:

    http://www.alistapart.com/articles/keepingcurrent/

    Which teaches you how to keep the current id using php for your navigation menu and goes fairly indepth. I haven’t used it yet, but will consider it as I have yet to find an answer on how to do it with CSS that works in WordPress.

    See with static pages it’s easy beacuse you place the html code for the navigation in one page, with WP you place it in the header of your theme, and this means that only one id would be highlighted.

    It would be nice if someone could provide an actual guide and not just a link to list-o-matic and say check out those examples. What I think I am struggling with is trying to understand how the thing works both with pages and with templates.

    Let’s say for example this is my code:

    <div id="navigation">
    <ul>
    <li><a href="#">Page One</a></li>
    <li id="currentpage"><a href="#">Page Two</a>
    </li>
    <li><a href="#">Page Three</a></li>
    <li><a href="#">Page Four</a></li>
    </ul>
    </div>

    That goes in the header.php correct? Now in my css I will create a style for navigation, stick in an id for current and active and place those ids within the first page of the code at the top, beause when a user visits a site the header is called and therefore the first page is currently home.

    What are the actual next steps? This is the bit that I find missing. How do you highlight your about page, your contact page and so on without creating multiple header.php files and then calling those? You don’t call the page in css, you call the attribute so if you create a page in WP, then you can’t assign an attribute to it if you use the same template for each page (which is the most likely).

    Now say you have a template, you all the header.php but unless you create a seperate header.php you won’t be able to highlight that template as the current.

    If someone has a better explanation and solution I’m all ears, but it’s something a lot of people (including myself) would like to learn. Has anyone thought of writing up a guide in the Codex because the implementation is slightly different to that of static pages, and I hope someone understands this and tries to explain it better.

    Thanks in advance.

    PS: If I’ve got it all wrong, let me know and explain where I should be looking and what I should be doing to get it working, because I’ve seen little in the way of workable examples, and people just giving and using standard non-highlighted navigation.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Avenir

    (@avenir)

    You’re on the right track here.

    Firstly, that block of code shouldn’t go in header.php. It should go wherever you want that navigation list to go. Then you should place some inline CSS in your header.php like this:

    <style type="text/css">
    #current { highlighting style... }
    </style>

    The way I do highlighting is by giving every item a unique id attribute. Then I write a little PHP conditional statement using WordPress’s is_ functions to check which link should be highlighted. So, say you give your three links id‘s one, two, and three. You could write:

    <?php if ( is_page('Page One') ) { $current = 'one'; }
    elseif ( is_page('Page Two') ) { $current = 'two'; }
    else ( is_page('Page Three') ) { $current = 'three'; }
    ?>

    And then I change the #current in the CSS to #<?php echo $current; ?> which will dynamically choose which page to highlight.

    Read about the is_ functions

    moshu

    (@moshu)

    I like it! and thanks (because I can read 100 times the post about the function I still am not able to come up with one line of code… LOL)
    So, where should one put this little “conditional statement”?

    Avenir

    (@avenir)

    Put the conditional statement in the header.php before the inline CSS block. That’ll ensure that the variable gets defined before it’s called for.

    Lorelle

    (@lorelle)

    Avenir, this question has been asked REPEATEDLY in the forums and while I can do this in my sleep in static HTML, working around the Loop to do this has been painful. Would you please write this up and post it on the Codex? If you have problems with that, let me know and I’ll help. This is brilliant! I’m going to use it immediately, but I, too, can only guess as to where to put the IF statement.

    Avenir

    (@avenir)

    Lorelle, if you could post it in the Codex, that’d be great! I have an account at the Codex but for the life of me I can’t figure out how to make a new page. You could start one and I’ll find it and finish it, maybe?

    [Edit] Stupid me. I figured it out. I’ll write this up when I have some free time!

    Thread Starter jinsan

    (@jinsan)

    Great stuff Avenir and I echo Lorelle – you’re the first, I believe, that has come up with a WP related answer!

    Cheers (though I’m so far into doing it the hard way that I don’t think I’ll use this lol). So can this be used to say style each pages highlight differently? And how far can the styling go?

    Say there are three pages, all with different styles in terms of colour with the menu three different colours, and you want the current to be highlighted in those respective colours, with a bit of work this could still work right?

    Avenir

    (@avenir)

    Yea I think it could still work. You might do something with descendant selectors. Umm….

    Say you broke menu up like this:

    <div id="nav">
    <ul id="menu1">
    <li id="one">blah</li>
    </ul>
    <ul id="menu2">
    <li id="two">bloop</li>
    </ul>
    <ul id="menu3">
    <li id="three">blar</li>
    </ul>
    </div>

    You could write the style so that you have my other stuff and but add other selectors for the three different menus:

    <style type="text/css">
    #menu1 #<?php echo $current; ?> { special menu1 highlighting }

    #menu2 #<?php echo $current; ?> { special menu2 highlighting }

    #menu3 #<?php echo $current; ?> { special menu3 highlighting }
    </style>

    And then it would style the thing differently based on whichever menu is its ancestor. You’d still need to give each list item a unique id or else you’d get some confusion. I’m not really sure if this would work, but it looks to me like it should. It’s a little late for me though, so forgive me if it actually blows up.

    I guess this is only limited by how much you’re willing to tax your brain in determining the inheritence and cascading. Personally, I’m not willing to go any further than this (and in fact I probably would never go even so far as this) — way too much thought.

    Beel

    (@beel)

    If I were to change what I have I’d probably leave the #current style in the css and use:
    <?php if (is_page(‘pagename’)) echo ‘id=”current”‘; ?> in each of the link tags.

    Lorelle

    (@lorelle)

    If you are using “Pages”, a css class for “current item” has been…..revealed. This gives the same special class for the “Page” you are on being highlighted. YAHOOOO

    http://codex.wordpress.org/Template_Tags/wp_list_pages

    Lorelle

    (@lorelle)

    I’ve skimmed through the article and it looks great, but can we also do the same thing with categories? That’s what is MOST requested, a kind a breadcrumb effect.

    The word “pages” gets everyone screwed up because we’re not sure if we’re talking about pages, posts, posts about pages, pages about posts, or categories….anyway, the most help on this would be highlighting the category the post is in.

    BRILLIANT!

    Beel

    (@beel)

    Shoot I thought that might work for me, but I have a menu of just the parent pages and a sidebar list of the subpages so unfortunately this creates 2 instances of id=”pagenav” – I’ll have to stick with my manually created parent menu… but wait, looks like one can omit that wrap.

    Beel

    (@beel)

    Apparently one can but running wp_list_pages twice in the same page causes errors. “invalid foreach” errors on my subpages list with those that do not themselves have subpages… bummer..

    Ok, I’m just getting around to trying to implement this on my site. Avenir, could you explain what you mean when you said “The way I do highlighting is by giving every item a unique id attribute. ” Or, rather, how you did this. I understand what you mean, but I can’t figure out how to give each item an id. Or maybe you entered the list manually, instead of using wp_list_pages?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘keeping current navigation highlighted’ is closed to new replies.