Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Conditional list of child pages in WordPress

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #36139
    hblackett
    Member

    Hi,
    I’m trying to modify the following piece of code:

    
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children); { ?>



    Currently I’m using the above to output a list of child pages on my website. That’s working fine.
    However, I want to add the conditional statement that the list should only appear if there is more than one child page.
    i.e. If there is only one child page there should be no list.

    Thanks, any advice would be very gratefully received!

    #94736
    TheDoc
    Member

    A very interesting request!

    Let me see here… This is a mashup of some things I’ve found, not sure if it’ll work but hopefully it sends you in the right direction:


      $count = 0;
      $pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
      foreach($pages as $page) {
      $count++;
      if ( $count > 1 ) { ?>
    • ID) ?>">post_title ?>

    • }
      ?>
    #94749
    hblackett
    Member

    Thanks for the reply, Doc, glad it caught your interest!

    With your code it does solve the problem of not displaying a list when there is only one child page. However, when there are two or more child pages it doesn’t output the first child page in the list, but continues to output the rest of the list as normal. Curious!

    I’m going to look into things and try for a workaround, but if anyone does find a solution in the meantime that would be enormous help.

    #94758
    TheDoc
    Member

    Aaahhh yes… Well then maybe you could just do this:


      $count = 0;
      $pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
      foreach($pages as $page) {
      $count++;
      if ( $count = 2 ) {
      wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
      }
      }
      ?>

    Again, not my realm of expertise, but my thinking here is “if the count gets to 2, then display child pages.

    #94882
    hblackett
    Member

    Hmm, have just tried the above and it doesn’t display anything. Thanks for your help though & pointing me in the direction of $count (I hadn’t used that before), it’s much appreciated. I’m also looking at this thread and will surely find a solution soon. Will post back when I do!

    #95013
    hblackett
    Member

    I’ve found a working solution so thought I’d share. Not sure if it could be streamlined at all, but it does the job for me so I’m happy to sign off on this, hope it’s of use for others.

    	
    $include_page_ids = array( $post->ID );
    for ($i = 0;$i < count($include_page_ids);$i++) {

    $children = get_pages('child_of='.$include_page_ids[$i]);
    if (count($children) > 1) {
    $sub = "";
    echo $sub;
    }

    }
    ?>
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.