is_subpage() – Custom Conditional Function
— Quick conditional function for WordPress to check if the current page is a sub page. It works both inside and outside the main loop, only for 2.5 and upwards.
→ Download the function here
Example
Same way as you use is_page, is_home, etc.
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php if ( is_subpage() ): ?> <p> This is a subpage </p> <?php endif ?> <?php endwhile; ?>
The Function
function is_subpage()
{
global $post, $wpdb;
if ( is_page() AND isset( $post->post_parent ) != 0 )
{
$aParent = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d AND post_type = 'page' LIMIT 1", $post->post_parent ) );
if ( $aParent->ID ) return true; else return false;
}
else
{
return false;
}
}
Im not sure why WordPress didn’t include a function like this by default, i hope you find it useful.
Cheers!
→ If you find this post useful please consider inviting me a cup of tea :) Thanks!











It does look very simple and useful. You could always submit it to the WP repository so that it might make it’s way into a future release. The tool is only as good as those who create it and those it inspires to continue that process.
I would assume that the reason it’s not a function is because it’s easier and more efficient to use !is_home() than to write another function that basically does the same thing.
Thanks both for the comments.
@JamieO, will give it a try thanks ;)
@Trevor !is_home() only lets you know if you are in a location that it’s not the home page. Could be a tag page, a search page, a category page and even a single page. It wont tell you if your are specifically in a sub page, which that is what this function does.
@Matt-
Ah ok, I guess I didn’t look close enough at the function to see what it does. It looks like they have updated the Codex recently, and it tells you how to test for a subpage
@Trevor no problem, thanks for the link.
Hey Matt, cool stuff!
You should consider making one big page for all of these, and having a download with “Matt Varones WordPress functions.php” or something to that extent :)
@Joost Thanks, glad you like it. Sounds like a good idea, thanks for the tip as I might have to do something like that.
Thanks a lot for this, I feel its shoddy work on the wordpress development team for not already having this function included within the source.
Thanks very much. It helps me for my new WP theme.
Very nice, it would’ve been helpful a few days ago, I had to develop a folding menu and I needed to test if there was a parent. I did it with get_page($post->post_parent). I hope you don’t mind if I add you to my “Friends” block on my site.
Regards,
Thanks Elliot, glad it might come handy the next time. Please go ahead, would be honored.
Happy new year!
and Happy New Year to you too!
this was exactly what i needed! thanks matt. will definitely be useful on many a project. :)
Any chance you might know how to make this function so that it can handle is_subpage(8) type of queries?
In the codex they mention something about testing for a subpage of a particular page using $post->post_parent == ‘11′ but I can’t get it to work…
Hi Nathan. A sub page is stilla page, so If you try checking for both a page ID and subpage it might do the trick for you:
if ( is_page(8) && is_subpage() )
{
// do your stuff here.
}
Cheers!
Nice post. It helped me alot. Thanks,
@Nathan: the codex gots it, as Trevor mentions above. Here’s their example:
post_parent == ‘2′ ) {
$bannerimg = ‘home.jpg’;
} elseif ( is_page(’learning’) || $post->post_parent == ‘56′ ) {
$bannerimg = ‘teaching.jpg’;
} elseif ( is_page(’admissions’) || $post->post_parent == ‘15′ ) {
$bannerimg = ‘admissions.jpg’;
} else {
$bannerimg = ‘home.jpg’; // Fall-through
}
?>
http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages
Hello!
Very Interesting post! Thank you for such interesting resource!
PS: Sorry for my bad english, I’v just started to learn this language ;)
See you!
Your, Raiul Baztepo
Is there any reason not to use the post->ancestors property?
I did a similar approach and found out later that there is an array in the post objects containing all ancestors. I simply count it’s size:
If it’s bigger than 0 it’s a subpage, bigger than 1 a subsubpage, etc.
Hi,
Thanks for the code – I was wondering how I could check if I’m on a grandchild (sub-sub-page) of page 10 for example. I would be so grateful for any advice. Thanks!