WordPress.org

Ready to get started?Download WordPress

Forums

Displaying only the direct children of a parent page ( sidebar nav list) (9 posts)

  1. joncalex
    Member
    Posted 5 years ago #

    Here is my code:

    <?php
      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) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?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.

    Thanks!

  2. Tammy Hart
    Member
    Posted 5 years ago #

    This is what you're looking for: WordPress: How to List Child Pages in Sidebar

  3. sudhishchemmur
    Member
    Posted 5 years ago #

    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

    <?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;

    ?>

    <?php

    $querystr = "SELECT post_parent from wp_posts where ID=".$post_ID;
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    echo '
    ##'.$post_parent.'##';
    ?>

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

  4. s_ha_dum
    Member
    Posted 5 years ago #

    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.

  5. gabesands
    Member
    Posted 5 years ago #

  6. Maxaud
    Member
    Posted 5 years ago #

    Try this:

    <?php
    	if($post->post_parent)
    		$children = wp_list_pages("sort_column=ID&amp;depth=1&amp;title_li=&amp;child_of=".$post->post_parent."&amp;echo=0");
    	else
    		$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 } ?>
  7. kapiljain.in
    Member
    Posted 5 years ago #

  8. mayoosuf
    Member
    Posted 5 years ago #

    @kapiljain.in

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

  9. pvf
    Member
    Posted 5 years ago #

    Hello!!
    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....

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags