Dynamic sidebar nav with current page highlighting

My sidebar loops through all of the child pages based on the parent page. For example, if you’re in the “About” section you’ll only see “About” child pages in the sidebar.

My problem is that I have no way of highlighting the current page. For example, if you’re on the child page “Our Team” (under “About”) I’d like to give the sidebar “Our Team” a tag link a “current-page” class.

Read More

Is there a way to add a “current-page” class dynamically to the child page you’re on?

<?php 
if ( $post->post_parent == '4' ) {
    query_posts("post_type=page&post_parent=4&orderby=menu_order&order=asc"); 
} elseif ( $post->post_parent == '6' ) {
    query_posts("post_type=page&post_parent=6&orderby=menu_order&order=asc");
} elseif ( $post->post_parent == '8' ) {
    query_posts("post_type=page&post_parent=8&orderby=menu_order&order=asc"); 
} elseif ( $post->post_parent == '10' ) {
    query_posts("post_type=page&post_parent=10&orderby=menu_order&order=asc"); 
}
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
...

Related posts

Leave a Reply

1 comment

  1. I see that you are have a hard time on this so here:

    <?php 
        global $post;
        $curent_post = $post;
        $curent_post_id = $post->ID
        $parents = array('4','6','8','10');
        if (in_array($post->post_parent,$parents)){
            query_posts("post_type=page&post_parent=".$post->post_parent."&orderby=menu_order&order=asc"); 
        }
        if ( have_posts() ) : while ( have_posts() ) : the_post();
        ?>
        <a href="<?php the_permalink(); 
        if ($curent_post_id = $post->ID){
            echo ' class="highlight"';
        }
        ?>"><?php the_title(); ?></a>
    

    and at the end of your loop add

    wp_reset_query();
    $post = $curent_post;
    

    no after that i would like to say that you really should use WP_Query instead of query_posts any time that its not the main query in the page.

    and when I’ll get back I will post and example using wp_list_pages()