Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out how to set up a conditional statement to check if the parent, child or grandchild is being displayed. This is what I've got so far:

<?php 

if ( is_page( 'about' ) || '29' == $post->post_parent ) {
    //Start of the custom code...
    while(has_sub_field('site_slideshows','option')):
    ?> <!-- Closing the PHP tag -->

        <div class="slideshow-slide" style="background-image:url(<?php the_sub_field('site_slideshows_slide_image'); ?>)">
            <div class="block-text" style="color: <?php the_sub_field('site_slideshows_slide_text_colour'); ?>;"><?php the_sub_field('site_slideshows_slide_text'); ?></div>
        </div>

    <?php //Opening the PHP tag again
    endwhile;

} elseif ( is_page( 'contact' ) || '31' == $post->post_parent ) {
    //Start of the custom code...
    while(has_sub_field('two_site_slideshows','option')):
    ?> <!-- Closing the PHP tag -->

        <div class="slideshow-slide" style="background-image:url(<?php the_sub_field('two_site_slideshows_slide_image'); ?>)">
            <div class="block-text" style="color: <?php the_sub_field('two_site_slide_text_colour'); ?>;"><?php the_sub_field('two_site_slideshows_slide_text'); ?></div>
        </div>

    <?php //Opening the PHP tag again
    endwhile;

}
?>

This will successfully check for the parent and child page, e.g. the about page and all the children of the about page, but how would I adjust these statements to include grandchild pages also? Any suggestions would be greatly appreciated.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Use get_post_ancestors( $post ). It returns an array of post IDs. The parent ID is the first entry, the grand parent the second and so on.

$ancestors = get_post_ancestors( $post );

if ( is_page( 'about' ) || in_array( '29', $ancestors ) )
{
    echo "This is About or one of its descendants";
}
elseif ( is_page( 'contact' ) || in_array( '31', $ancestors ) )
{
    echo "This is Contact or one of its descendants";
}
share|improve this answer
    
thanks for that, I'm not sure that's what I'm after though. As seen above if ( is_page( 'about' ) || '29' == $post->post_parent ) { allows me to display the same content on the parent page about and all it's children, I just want to add grandchildren into this if statement somehow, thus displaying the same content on the about page and all it's children and grandchildren. The same with the elseif statement for the contact page etc etc. –  user1374796 Jun 26 '13 at 19:36
    
@user1374796 See my update to illustrate how to use the ancestors in your situation. –  toscho Jun 26 '13 at 20:13
    
@user1374796 This answer might help too. You could store the data to display in a custom field an drop the hard coded post IDs. –  toscho Jun 26 '13 at 20:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.