WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to only list pages if they have no children (8 posts)

  1. carolineh
    Member
    Posted 4 years ago #

    Hey people, I'm snowed off work so I've been in this forum a lot over the last few days, hope people are taking advantage of the mayhem a bit of snow causes (in the UK at least) lol.

    I'm building a toggle menu in jquery using wp_list_pages, so far so good but for what I am doing I need to list the pages with children and the pages without children separately, due to adding a class in the top level pages that allows the subpages to slide down, but not needed for those without children.

    I found this post solved the problem of only displaying pages with children:

    http://wordpress.org/support/topic/349379

    I now need to only display pages that don't have children. I'm sure this is very simple, and I would be really grateful if somebody could actually talk me through the code too. The part I don't really understand is to do with post.parents:

    posts.post_parent

    I have read a lot of information in the codex relating to how to only show subpages when in the parent page, or only when on child pages from the same parent, but I want this menu to be the same no matter where viewed from, except with dynamic highlighting, but I'll cross that bridge when I come to it!

    Many Thanks.

  2. MichaelH
    Member
    Posted 4 years ago #

    Try:

    get_pages('parent=0');

    Function_Reference/get_pages

  3. carolineh
    Member
    Posted 4 years ago #

    Hi Michael,

    I tried this:

    <?php
    $pages = get_pages('parent=0');
    
    foreach ($pages as $page) {
      	?>
    <li><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title;
                                                 ?>
    </a></li>
    	<?php } ?>

    but it gives me the same results as using the depth parameter in wp_list_pages.

    parent
    (integer) Displays those pages that have this ID as a parent. Defaults to -1 (displays all Pages regardless of parent). Note that this can be used to limit the 'depth' of the child_of parameter, so only one generation of descendants might be retrieved. You must use this in conjuction with the child_of parameter. Feed it the same ID.

    * -1 - default, no parent restriction
    * 0 - returns all top level pages

    From the get_pages reference,
    I'm not sure if I'm using it in the wrong way! lol

  4. carolineh
    Member
    Posted 4 years ago #

    I was also trying something like this:

    <?php 
    
    $nochild = $wpdb->get_col("SELECT DISTINCT post_parent FROM $wpdb->posts WHERE $wpdb->posts.post_parent = 0  AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish'");
    
    foreach ($nochild as $nochildpage) {
      	?>
    <li><a href="<?php echo get_page_link($nochildpage->ID) ?>"><?php echo $nochildpage->post_title;
                                                 ?>
    </a></li>
    	<?php } ?>

    using the code that displays only posts with children.

  5. MichaelH
    Member
    Posted 4 years ago #

    Ooh darn, you wanted pages with no children....

    <?php
    //get all 'first level' pages, then iterate through those results,
    //then for each result, see if there are child pages, and if there
    //are no child pages for that page, then display
    $pages = get_pages('parent=0');
    foreach ($pages as $page) {
      $child = get_pages('child_of='.$page->ID);
      if (empty($child)) {
        ?>
      	<li><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title;?></a></li>
        <?php
      }
    }
    ?>
  6. carolineh
    Member
    Posted 4 years ago #

    Thanks Michael!

    So you have said only display parent pages,
    then for each page you have got all the children,
    then said if there are no children display the list!

    That is amazing.

    I was just having a look in the database, and was wondering what the post_parent numbers represent in the wp_posts table?

    I'm also still a bit flumoxed by this part!

    ->
    as in
    ('child_of='.$page->ID)

    Thanks for your help :)

  7. MichaelH
    Member
    Posted 4 years ago #

    In wp_posts, when talking about Pages, post_parent is the ID of the parent page.

    Regarding

    foreach ($pages as $page) {
      $child = get_pages('child_of='.$page->ID);

    If you put echo "<pre>"; print_r($page); echo "</pre>"; between those two lines you will see that $page->ID is one element of the $page array that represents the data for one page.

  8. carolineh
    Member
    Posted 4 years ago #

    Hi Michael, Thanks for the explanation and for solving my problem.

    :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.