Support » Themes and Templates » “On-state” for current page in navigation

  • Hi. I am using the page system to create a series of pages for a client (obviously).

    I have the pages dynamically generating a navigation menu using the <?php wp_list_pages(‘title_li=’); ?> tag.

    So far it’s great. However, I need to generate an ‘on state’ for the current page in the nav menu. Right now the link it generates looks like this:

    <li class=”page_item”> -a href=”″ title=”About Us”- About Us -/a-

    What i need is to add a ‘onstate’ class to that ‘a’ tag if this is the current page.

    I hope this makes sense. I am not a super great coder so please be gentle. Thanks again guys.


Viewing 7 replies - 1 through 7 (of 7 total)
  • You can create a class name for your body, and then in each page have the link associated with that page have the altered state for it’s button. It’s pretty easy to do, but you need to edit your CSS file.

    So, for example, for page 1, you’d have in your HTML:

    <body class=”page1″>

    And in your CSS, you’d have the regular styled anchor links – but you’d separate your “page 1” anchor link in the css like so:

    body#page1 a, body#page1 a:link {
    style for the link here


    You just have to read more carefully the Codex documentation:
    See Section 3.
    WP automatically adds an additional class to the page_item you are on –> current_page_item.
    Style it…

    Hrm… not sure I’m explaining this the right way.

    the default output for the <?php wp_list_pages(‘title_li=’); ?> tag is this for every link: (sorry, i’m using brackets for the a link)

    <li class=”page_item”>[a href=”″ title=”About Page”]About[/a]

    If I have 5 pages, they all end up with that exact same markup. Now if I’m page 4, the about page, for example, and I want that single page to have a different background in the navigation… I’m going to need to add an ‘onstate’ class to the releveant a link.

    So wordpress will have to know that I am currently on page 4, and that since i’m on page 4, it needs to style the link to page 4 in the navigation differently by adding a class to the a tag.

    I have been looking for plugins to do this, but i don’t see any. It’s pretty standard user interface stuff to want to have your navigation display like this. I’ve been poking around and trying to re-format the ‘template_functions_post’ file which contains the actual function i’m looking at:

    function wp_list_pages($args = ”) {
    parse_str($args, $r);
    if ( !isset($r[‘depth’]) )
    $r[‘depth’] = 0;
    if ( !isset($r[‘show_date’]) )
    $r[‘show_date’] = ”;
    if ( !isset($r[‘child_of’]) )
    $r[‘child_of’] = 0;
    if ( !isset($r[‘title_li’]) )
    $r[‘title_li’] = __(‘Pages’);
    if ( !isset($r[‘echo’]) )
    $r[‘echo’] = 1;

    $output = ”;

    // Query pages.
    $pages = & get_pages($args);
    if ( $pages ) {

    if ( $r[‘title_li’] )
    $output .= ‘<li class=”pagenav”>’ . $r[‘title_li’] . ‘


    // Now loop over all pages that were selected
    $page_tree = Array();
    foreach ( $pages as $page ) {
    // set the title for the current page
    $page_tree[$page->ID][‘title’] = $page->post_title;
    $page_tree[$page->ID][‘name’] = $page->post_name;

    // set the selected date for the current page
    // depending on the query arguments this is either
    // the createtion date or the modification date
    // as a unix timestamp. It will also always be in the
    // ts field.
    if ( !empty($r[‘show_date’]) ) {
    if ( ‘modified’ == $r[‘show_date’] )
    $page_tree[$page->ID][‘ts’] = $page->post_modified;
    $page_tree[$page->ID][‘ts’] = $page->post_date;

    // The tricky bit!!
    // Using the parent ID of the current page as the
    // array index we set the curent page as a child of that page.
    // We can now start looping over the $page_tree array
    // with any ID which will output the page links from that ID downwards.
    if ( $page->post_parent != $page->ID)
    $page_tree[$page->post_parent][‘children’][] = $page->ID;
    // Output of the pages starting with child_of as the root ID.
    // child_of defaults to 0 if not supplied in the query.
    $output .= _page_level_out($r[‘child_of’],$page_tree, $r, 0, false);
    if ( $r[‘title_li’] )
    $output .= ‘

    $output = apply_filters(‘wp_list_pages’, $output);

    if ( $r[‘echo’] )
    echo $output;
    return $output;

    so somewhere in there I should be able to tell it, ‘if this is the current page’ then ‘output this class in the link’.

    Thanks for your help people!

    Yes, you’ve explained it just fine in the first post. You have several pages, and there are links associated with each page. You want the end user to know which page they are currently on by having the link associated with that page to be styled differently than the others. This is something commonly done with tabbed naviagtion layouts, as well.

    As moshu said, look in your code for what classes the system is giving your bbody tag, and edit your CSS to reflect the change as I’ve stated above.

    To your anchor tags, simply add a class of “nav” to every one of them. So when the page comes up, it’ll see <body class=”page1″>, then find the CSS associated with it:

    body#page1 a.nav, body#page1 a.nav:link {
    style here

    And that’s all there is to it.

    You should read again what I posted and follow the link. Additionally, while being on a Page of yours, take a look at the source code if you don’t get what I am trying to explain 🙂

    Ok, perfect. I see what you’re saying now. I was running into a problem because I’m trying to get one of those pages to be the static home page. In that situation, it’s not displaying the current_page_item class. interesting.

    Yes, that seems to be an issue with the static frontpage plugin (or WP, not sure which). I’ve run into it before too.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘“On-state” for current page in navigation’ is closed to new replies.