Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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>
...
share|improve this question
1  
Why not use wp_list_pages? –  Bainternet Mar 15 '11 at 1:55
    
I don't understand how that would add the highlighting class. –  Ryan Mar 16 '11 at 19:06
    
It ads current class and you can style based on that. And if that'd not enough it has a filter –  Bainternet Mar 16 '11 at 19:21
1  
Here's the answer: wordpress.org/support/topic/… –  Ryan Mar 16 '11 at 22:14

1 Answer 1

up vote 1 down vote accepted

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()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.