WordPress has_subpage() and Other Useful Parent/Child Page-related Functions

Want to detect whether a specific Page on a WordPress install has children or not?

This function comes in handy, just add it to your functions.php:

// conditional statement for detecting if a page has sub-pages
function has_subpage() {
global $post;
if($post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
} if ($children) {
return true;
} else {
return false;
}
}

And you know that bit of code we’ve all been using for years, the one that displays sub-pages whether you’re on the parent or one of its child pages? Well here, now it can be a function, too:

// display sub-pages even when on a page
function wp_section_pages() {
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php }
}

Up Next: Checkmarks and Xs with Pure CSS (Using Pseudo Elements)