Using this piece of code you can show content depending if it’s a child-page or not:

<?php
global $post;

if ( is_page() && $post->post_parent ) : ?>
This is a child-page.

<?php else : ?>
This is a parent-page.

<?php endif; ?>

But I would like to add one more statement, so that I could have different content if it’s a parent-page that has a child, or a parent-page that hasn’t. Would something like this below work? If yes, what would “XXX” be?

<?php
global $post;

if ( is_page() && $post->post_parent ) : ?>
This is a child-page.

<?php elseif ( is_page() && XXX ) : ?>
This is a parent-page (with one or more children)

<?php else : ?>
This is a parent page without children.

<?php endif; ?>

Thanks in advance!

3 Answers
3

I ended up using this code:

<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );

if ( is_page() && $post->post_parent ) : ?>
This is a child-page.

<?php elseif ( is_page() && count( $children ) > 0 ) : ?>
This is a parent-page (with one or more children)

<?php else : ?>
This is a parent page without children.

<?php endif; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *