Wordpress - Function to return true if current page has child pages
Your above function tests whether a page is a child page of some other page, not whether it has children.
You can test for children of the current page like so:
function has_children() {
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if( count( $children ) == 0 ) {
return false;
} else {
return true;
}
}
Further reading:
- wp codex: get_pages
- php manual: count
Here is version for any post type, in case if you are using custom post type
function has_children($post_ID = null) {
if ($post_ID === null) {
global $post;
$post_ID = $post->ID;
}
$query = new WP_Query(array('post_parent' => $post_ID, 'post_type' => 'any'));
return $query->have_posts();
}