Support » Developing with WordPress » Displaying only the direct children of a parent page ( sidebar nav list)

  • Here is my code:

      if($post->post_parent) {
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); }
      else {
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); }
      if ($children) { ?>
      <?php echo $children; ?>
      <?php } ?>

    This outputs children and grandchildren links. I want to only display the direct children for a particular page, which I will wrap in a conditional. So what I need to do is tell it not to display the grandchildren. I hope this makes sense.

    Anyone have the expertise to figure this out? I’ve had trouble searching for this online.


Viewing 8 replies - 1 through 8 (of 8 total)
  • This is what you’re looking for: WordPress: How to List Child Pages in Sidebar

    I am looking for reverse functionality.

    I have a page by name ‘Parent Page’ and there are 5 child pages say ‘Page A’,’Page B’,’Page C’,’Page D’,’Page E’ which is under ‘Parent Page’. I want to get the parent page name listes when I am within any child page. I tried many and finally decided to write a custom code in sidebar.php

    $post_obj = $wp_query->get_queried_object();
    $post_ID = $post_obj->ID;
    $post_title = $post_obj->post_title;
    $post_name = $post_obj->post_name;
    $post_ParentPage = $post_obj->post_parent;



    $querystr = “SELECT post_parent from wp_posts where ID=”.$post_ID;
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    echo ‘

    The problem is that the $post_parent always displays null. Can someone help me on this?

    sudhishchemmur… you can’t really ‘echo’ an object, which is what you’ve asked wpdb to return. Try print_r($post_parent) and see what you get.

    Possibly, what you want to do is echo $post_parent->ID.

    Alternately, you could use $wpdb->get_var which will return a plain vanilla variable rather than an array or an object. If you use get_var you should be able to just echo it out.

    Try this:

    		$children = wp_list_pages("sort_column=ID&amp;depth=1&amp;title_li=&amp;child_of=".$post->post_parent."&amp;echo=0");
    		$children = wp_list_pages("sort_column=ID&amp;depth=1&amp;title_li=&amp;child_of=".$post->ID."&amp;echo=0");
    	if ($children) { ?>
    		<ul class="child"><?php echo $children; ?></ul>
    <?php } ?>

    its a nice code snippet, what I was searching for, and just I got it. thanks!

    BTW, if possible an enhancement can be done as if the chilled pages not exist better not to display the title(parent page name).

    i think this is along the lines of what i have to do.
    in plain English:

    I need to SELECT the posts that have the same term_id and parent_id as the one being displayed.

    how would this be created in SQL?
    this would also need to be dynamic, with no category ids hardcoded….

    please i hope somebody can help me with this! i have spent to many hours trying to figure it out with no luck 🙁

    thank you in advance….

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Displaying only the direct children of a parent page ( sidebar nav list)’ is closed to new replies.