WordPress list parent-page and children / parent-title not showing on parent-page

currently i am using this code to print a list of a parent-page and its children.

        <?php
        if($post->post_parent){
            $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); 
            $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
        } else {
            $children = wp_list_pages("title_li=&include=".$post->ID."&echo=0");
            $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
        }
        if ($children) { ?>
        <div class="meta-box">
            <div class="nav-right">
              <ul>
                <?php echo $children; ?>
              </ul>
            </div>
        </div> 
        <?php } ?>

Everything works fine when i am on a child page. It prints the title of the parent page and the titles of its children.

Read More

But when i am on a parent page it only prints the children and not the title of the parent page.

What i want to achieve:

When on parent page:

  • Parent title (currently not showing)
  • Child 1
  • Child 2
  • Child 3

When on child page:

  • Parent title
  • Child 1
  • Child 2
  • Child 3

Any help would be greatly appreciated

Mat

Related posts

Leave a Reply

1 comment

  1. Your code works fine, you are missing a period in the ‘else’ rule

    <?php
            if($post->post_parent){
                $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); 
                $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
            } else {
                $children = wp_list_pages("title_li=&include=".$post->ID."&echo=0");
                $children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
            }
            if ($children) { ?>
            <div class="meta-box">
                <div class="nav-right">
                  <ul>
                    <?php echo $children; ?>
                  </ul>
                </div>
            </div> 
            <?php } ?>