WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

if ( $post->post_parent == '4' ) {
} elseif ( $post->post_parent == '6' ) {
} elseif ( $post->post_parent == '8' ) {
} elseif ( $post->post_parent == '10' ) {
if ( have_posts() ) : while ( have_posts() ) : the_post();
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
share|improve this question
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
Here's the answer: wordpress.org/support/topic/… – Ryan Mar 16 '11 at 22:14
up vote 1 down vote accepted

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

    global $post;
    $curent_post = $post;
    $curent_post_id = $post->ID
    $parents = array('4','6','8','10');
    if (in_array($post->post_parent,$parents)){
    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

$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


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.