• Hey guys,
    I’m trying to sort a solution out for my template
    I basically want to check on the page if a certain subpage exists of the current page
    so for example –

    business1
    -business1 employees
    business2
    business3
    -business3 employees

    If the subpage ‘businessX employees’ exists I want to include some info.
    else don’t.
    I’ve had a look around and can’t find out anything that would work.
    Can anyone help?
    Cheers
    Rich

Viewing 15 replies - 1 through 15 (of 15 total)
  • Try using the get_posts() function, with the post_parent argument.

    For example, from inside your Loop:

    global $post;
    $pagequeryargs = array( 'post_parent' => $post->ID );
    $childpages = get_posts( $pagequeryargs );
    if ( $childpages ) {
         // the current Page has Child pages; do something!
    }
    Thread Starter richardhomer

    (@richardhomer)

    Cheers Chip
    I need it to check for a particular child page though rather than if it has any child pages.
    That’s what I seem to be having problems with

    Well, you can do that, too. Inside of the if ( $childpages ) conditional, you can look for your specific Page. e.g.:

    foreach ( $childpages as $childpage ) {
         if ( 'business1-employees' == $childpage->post_name ) {
              // This is the Page I'm looking for; do something!
         }
    }

    You could also match “Business1 Employees” to $childpage->post_title, or you could match by Page ID using $childpage->ID.

    Thread Starter richardhomer

    (@richardhomer)

    Ok Thanks Chip, I’ll give this a go and see if I can get it working!
    Cheers

    Thread Starter richardhomer

    (@richardhomer)

    Hey Chip, Not sure how to implement this actually into my loop, can you help?

    Hey Chip, Not sure how to implement this actually into my loop, can you help?

    Without knowing exactly how/where you want your sub-page content to appear, it is difficult to give specific help.

    But, for example, let’s say that you want to output the content of the current Page, followed by whatever you’re pulling from your Child Pages.

    Inside your Loop, you call the_content(), which outputs the content of the current Page.

    Anywhere after that call, but before your endwhile declaration, place your get_posts() code.

    Thread Starter richardhomer

    (@richardhomer)

    hmm ok thanks.
    Basically I need to add a
    <li> item if the page is present then add a section below with the content of the page
    How does this look?

    <?php
    		 global $post;
    $pagequeryargs = array( 'post_parent' => $post->ID );
    $childpages = get_posts( $pagequeryargs );
    foreach ( $childpages as $childpage ) {
         if ( 'business1-employees' == $childpage->post_name ) {
                  echo '<li>Yes</li>';
         }
    }
    
    ?>

    Other than not being well-formed HTML (you’ve got list items, not wrapped in list container tags), that should work.

    I’d do something like:

    <?php
    		 global $post;
    $pagequeryargs = array( 'post_parent' => $post->ID );
    $childpages = get_posts( $pagequeryargs );
    foreach ( $childpages as $childpage ) {
         if ( 'business1-employees' == $childpage->post_name ) { ?>
                  <div class="subpage-section">
                  <?php echo $childpage->post_content; ?>
                  </div>
         <?php }
    }
    
    ?>

    Thread Starter richardhomer

    (@richardhomer)

    Cheers Chip.
    I’ve tried this now (substituting business1-employees for the actual page title) and it doesn’t seem to work.

    any clues?
    Thanks

    Try adding all appropriate arguments to your argument array:

    $pagequeryargs = array(
         'post_parent' => $post->ID,
         'post_type' => 'page',
         'numberposts' => -1,
         'orderby' => 'menu_order',
         'order' => 'ASC',
         'post_status' => null
    );

    Thread Starter richardhomer

    (@richardhomer)

    Ahh perfect!
    Works a treat!
    Big thanks Chip, much appreciated
    Rich

    Glad to help!

    Thread Starter richardhomer

    (@richardhomer)

    I’m trying to further this – basically I need to call the pages under the specified child page –

    [Code moderated as per the Forum Rules. Please use the pastebin]

    This is what I’d use out of the loop but it’s duplicating content which is obviously not wanted.
    I’ve tried to use get_posts but not sure how to do the ‘count’ I’d normally use with query_posts.
    any help is appreciated!

    Thread Starter richardhomer

    (@richardhomer)

    haha typical, spend hours trying to solve this then as soon as I post this, I figure it out!
    Sorry to bother anyone

    I’ve tried to use get_posts but not sure how to do the ‘count’ I’d normally use with query_posts.

    That would be the numberposts array key:

    $pagequeryargs = array(
         'post_parent' => $post->ID,
         'post_type' => 'page',
         'numberposts' => -1, // this array key determines the number of posts
         'orderby' => 'menu_order',
         'order' => 'ASC',
         'post_status' => null
    );

    Like such:
    'numberposts' => -1 = show all posts
    'numberposts' => 10 = show 10 posts
    etc.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘If subpage exists?’ is closed to new replies.