Skip to content

Instantly share code, notes, and snippets.

@wpchannel
Last active February 18, 2018 19:08
Show Gist options
  • Save wpchannel/1abbcc8b647e6312ee3279a6f5f31514 to your computer and use it in GitHub Desktop.
Save wpchannel/1abbcc8b647e6312ee3279a6f5f31514 to your computer and use it in GitHub Desktop.
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