Quantcast
Viewing all articles
Browse latest Browse all 15

Determine if a WordPress post or page has children

Here’s a simple function to determine if a post, page, or custom post has children in WordPress. It works by getting the children of the current post and returning a count. It will return 0 (false) if there are no children and some positive integer (true) if there are children.

function has_children() {
	global $post;
	return count( get_posts( array('post_parent' => $post->ID, 'post_type' => $post->post_type) ) );
}

Use it in your page or post templates:

if ( has_children() ) {
	// do something if this item has children
}

The post Determine if a WordPress post or page has children appeared first on Scott Nelle.


Viewing all articles
Browse latest Browse all 15

Trending Articles