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

This code works well to display child pages of the parent page while viewing both the parent page and child page. It doesn't apply the current_page_item CSS class to the parent while viewing it (and only it, not interested in it being applied while on the children) - any suggestions?

<ul class="subpages">
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) {
$parent_title = get_the_title($post->post_parent);?>
<li><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a></li>
  <?php echo $children; ?>
  <?php } ?>
share|improve this question
up vote 0 down vote accepted

If I understand what you want, it is trickier than it should be to get it working.

$ancestors = array();
$ancestors = get_ancestors($post->ID,'page');
$parent = (!empty($ancestors)) ? array_pop($ancestors) : $post->ID;
if (!empty($parent)) {
  $pages = get_pages(array('child_of'=>$parent));
  if (!empty($pages)) {
    $page_ids = array();
    foreach ($pages as $page) {
      $page_ids[] = $page->ID;
    $children = wp_list_pages("include=".$parent.','.implode(',',$page_ids)."&echo=1");
    echo '<ul>'.$children.'</ul>'; 

You have to get a list of all of your pages and pass them in the include parameter. The basic technique is listed in the Codex. The above is nearly a copy, in fact.

That will give you current_page_ancestor on all ancestor pages and current_page_parent on the immediate parent only. That should be sufficient.

If you notice I have included the top-level parent in the page list. Your code only does that sometimes. I don't know if that is by design or not, but I included it. You should be able to add or remove that page to match whatever logic you need.

share|improve this answer

Solution for your problem Try it



    .current-menu-item a {
        color: red;
    <ul class="subpages">
            $children = get_pages("child_of=".$post->post_parent);
            $parent_title = get_the_title($post->post_parent);
            $link = get_permalink($post->post_parent);
            $children = get_pages("child_of=".$post->ID);
            $parent_title = get_the_title($post->ID);
            $link = get_permalink($post->ID);
            $parent_page = $post->ID;
        if ($children) {
            <li <?php if( !empty($parent_page) && $parent_page==$post->ID){echo 'class="current-menu-item"';} ?>><a href="<?php echo $link; ?>"><?php echo $parent_title;?></a></li>
                foreach( $children as $post ) : setup_postdata($post); 
                    <li <?php if(is_page($post->ID)){echo 'class="current-menu-item"';} ?>>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

3.Note:Remove css style at top of my code when your test complete and apply your own css code.

share|improve this answer
Sorry, this one doesn't work. – AndrettiMilas May 18 '13 at 19:00
is there any error ? – Ravinder Kumar May 18 '13 at 19:24
i edited my code, working on my localserver try it.. – Ravinder Kumar May 18 '13 at 20:06

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.