How to Check if a WordPress Page Has Children/subpages

Strangely enough WordPress has no function for checking whether a page has children/subpages. This has to be checked using a somewhat more cumbersome method as highlighted below:

$children = get_pages('child_of='.$post->ID);?>
if( count( $children ) != 0 ) { // Has Children }
else { // No children }

OR

$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
    // This page has subpages
}

If anyone knows of a better way to do this I’d be interested to know. Until then, this is probably the best way to check whether a page has children or subpages.

Related posts:

  1. Formatting Archive Page Titles in WordPress
  2. Get Parent Page Title and Permalink in WordPress
  3. How to Set a Home Page and Blog Page on WordPress
  4. Enhancing WordPress’ wP_list_pages With Page Lists Plus
  5. Password Protecting Your WordPress Website or Blog

One Comment

Got something to say? Feel free, I want to hear from you! Leave a Comment

Trackbacks for this post

  1. Dos formas para saber si una página tiene subpáginas en WordPress | voragine.net

Leave a Comment

Let us know your thoughts on this post but remember to place nicely folks!