Check if a Page has any Children or Subpages

Written by Jean Galea
Written by Jean Galea
Code snippet that checks if a page has any children/subpages.

Partner Sponsors

BionicWP

Full disclosure: If you purchase through a link on our site, we may earn a commission. Learn more.

To check if a page has any children/subpages, insert the following into your functions.php file,

[php]

/*—————————————–
Check if a page has any children / subpages
—————————————–*/

function has_children($post_id) {
$children = get_pages("child_of=$post_id");
if( count( $children ) != 0 ) { return true; } // Has Children
else { return false; } // No children
}

[/php]

then in your template call it as follows (within an if statement usually):

[php]has_children($post->ID)[/php]

This article was filed in our archives.
Written by Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

In this article

Discover More

New discoveries, every week.
Join thousands of designers, developers, and builders that come to WP Mayor to find the best guides, tools, and services for their next website. One email, once a week.
WP Mayor Newsletter

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored just for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.

What's missing?