Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test if a page has children or not
<?php
$parent_ID = $post->post_parent ? @ array_pop(get_post_ancestors($post)) : $post->ID;
$children = wp_list_pages(
array(
'child_of' => $parent_ID,
'title_li' => '',
'echo' => false,
)
);
?>
<?php if ($children) : ?>
<h3 class="widget-title"><?php _e('Read also in this section', 'textdomain'); ?></h3>
<?php if ($post->post_parent) {
$children = wp_list_pages(array(
'depth' => 1,
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
));
} else {
$children = wp_list_pages(array(
'depth' => 1,
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
));
}
?>
<?php if ($children) : ?>
<ul>
<?php echo $children; ?>
</ul>
<?php endif; ?>
<?php endif ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.