WordPress.org

Forums

[resolved] Restricting child pages (18 posts)

  1. nadine00
    Member
    Posted 5 years ago #

    Hi there,

    I am using this code on a parent page to display my child pages as posts:

    http://codex.wordpress.org/Function_Reference/get_pages#Displaying_Child_pages_of_the_current_page_in_post_format

    Question: How do I keep this from showing sub-sub pages? I only want it to show the first level of sub pages, and nothing else.

    I went to look at get_page() to see about 'parent' or 'depth' but it is not working for me.

    Thanks.

    Nadine

  2. MichaelH
    Member
    Posted 5 years ago #

    <?php
    $gen1 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent = 0  AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
    $gen1_ids = implode($gen1,', ');
    $gen2 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen1_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
    $gen1and2_ids=array_merge($gen1,$gen2);
    $ids = implode($gen1and2_ids,', ');
    	$pages = get_pages('include='.$ids);
    	$count = 0;
    	foreach($pages as $page)
    	{
    		//$content = $page->post_content;
    		//if(!$content)
    		//	continue;
    		//if($count >= 2)
    		//	break;
    		//$count++;
    		//$content = apply_filters('the_content', $content);
    	?>
    		<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<div class="entry"><?php //echo $content ?></div>
    	<?php
    	}
    ?>
  3. nadine00
    Member
    Posted 5 years ago #

    Hmm. That's showing all the subpages.

    If I change this posts.post_parent = 0 to this posts.post_parent = 9 it just puts me back at the same spot: Lists all the children of page 9.

    I need to limit the sub pages so it only shows the first level of children, not the second level of children.

  4. nadine00
    Member
    Posted 5 years ago #

    Still tinkering with this. Any ideas? Sorry for the questions. :-/

    thanks.

  5. MichaelH
    Member
    Posted 5 years ago #

    So if you have:

    Page
    -Page child
    --Page grandchild
    ---Page great grandchild
    Page 2
    Page 3
    -Page 3 child
    --Page 3 grandchild

    You want to see only:
    Page
    -Page child
    Page 2
    Page 3
    -Page 3 child

    Correct?

  6. nadine00
    Member
    Posted 5 years ago #

    Close....

    If I have:

    Page
    -Page child
    --Page grandchild
    ---Page great grandchild

    I only want to display:

    -Page child

    Its a landing page. So, the landing page is the Parent page, and everything displayed is going to have always have the same parent.

    Nadine.

  7. MichaelH
    Member
    Posted 5 years ago #

    <?php
    $gen1 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent = 0  AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
    $gen1_ids = implode($gen1,', ');
    $gen2 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen1_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
    $gen2_ids = implode($gen2,', ');
    //don't really need these next two lines but left there for example
    $gen3 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen2_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
    $gen3_ids = implode($gen3,', ');
    wp_list_pages('include=' . $gen2_ids . '&title_li=<h2>' . __('Level 2 Pages') . '</h2>');
    ?>
  8. nadine00
    Member
    Posted 5 years ago #

    That's giving me only:

    --Page grandchild

    One level too far down.

  9. MichaelH
    Member
    Posted 5 years ago #

    Link to see the pages you are talking about please.

  10. nadine00
    Member
    Posted 5 years ago #

    http://work.nadinelessio.com/firstsecond/build/book-guides/

    The older code is in there, and I only have the titles in for testing. The FAQ Pages are sub pages of the Volume 1 and Volume 2.

    Trying to find a way so only Volume 1, and Volume 2 info shows up, but none of their children.

  11. MichaelH
    Member
    Posted 5 years ago #

    Over in your sidebar, please put the Pages widget or if not using widgets, put this in your sidebar.php

    <?php
    wp_list_pages('title_li=All pages');
    ?>
  12. nadine00
    Member
    Posted 5 years ago #

    Ok. Done.

  13. MichaelH
    Member
    Posted 5 years ago #

    So you are wanting this--when visiting the Book Guides page, only want to see:
    o Drawing Words & Writing Pictures Volume 1
    o Drawing Words & Writing Pictures Volume 2

  14. nadine00
    Member
    Posted 5 years ago #

    Yep. That's it. :)

  15. MichaelH
    Member
    Posted 5 years ago #

    <?php
    $page_id = $posts[0]->ID;
    $args=array(
      'post_parent' => $page_id,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  16. nadine00
    Member
    Posted 5 years ago #

    Thank you!

    Can you explain what you've done? I've never used caller_get_posts before. Also will wordpress 3.0 have simpler depth handling for get_pages() ?

    Thanks again for your help. That really had me stumped for a long time.

    cheers

    Nadine.

  17. MichaelH
    Member
    Posted 5 years ago #

    See the template tag, query_posts(), for arguments such as caller_get_posts.

    Don't think get_pages is changing in 3.0.

  18. atena86s
    Member
    Posted 4 years ago #

    Parent Id by default is -1 in post.php file.
    'parent' => -1

    So If you use this function : wp_get_pages("title_li=&child_of=$id&parent=$id");
    Where $id = your parent ID

    You will get the first level children.

Topic Closed

This topic has been closed to new replies.

About this Topic