Custom WordPress function to check page parent
— Quick update to the is_subpage() function. Basically it’s does the same but now it also accepts an integer to check if the current page is a child of certain ID. This way its easier to track if the current location is a subpage of a certain page. If it matches it will return a true value otherwise, it will be false.
→ Download the function here
function is_subpage( $iID = null )
{
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 ( is_int( $iID ) > 0 )
if ( $aParent->ID == $iID ) return true; else return false;
else
if ( $aParent->ID ) return true; else return false;
}
else
{
return false;
}
}
/* EXAMPLE */
if ( is_page('producs') OR is_subpage( 8 ) )
{
//do something
};
Cheers!
→ If you find this post useful please consider inviting me a cup of tea :) Thanks!




Hi Matt
I’m clearly doing something wrong, but I can’t figure this out.
Have added the is_subpage() function to my template theme, functions.php and am using widget logic control in an attempt to build context sensitive subpage menus in the sidebar.
Thus if I have two top level menu items called “home” and “about” I would want to show different sidebar menus in each. The conventional widget logic for the sidebar on the home page would then be “is_page(‘home’) || is_subpage()” and on the about page would be “is_page(‘about’) || is_subpage()” – and each instance would then show different subpages in different locations.
What actually happens is that the top level menu items show the correct context sensitive menu, but any sub pages show ALL submenus – both those from “home” and “about”.
I really don’t want to have to “name” each of the subpage instances explicitly in each usage since that renders it pointless having/using the subpage function in the first place.
Not being a programmer this may be the wrong description, but it feels as if your function is creating only one global instance of the subpage array and not clearing that array when a user moves to a different page/subpage.
Hope this description makes sense. Any help or pointers would be most useful.
Hi Robert, thanks for the comment. I understand your problem, what you need is to pass the ID also of that one top page you are checking. For example if you Home page ID is 3 and your About page ID is 5 it would be:
“is_page(’home’) || is_subpage(4)”
“is_page(’about’) || is_subpage(5)”
Otherwise it wil show you the subpages sidebar each time you are on a subpage no matter which parent page they have. Hope that helps!
Matt,
Very helpful function. I added a reference to it from the WordPress Codex.
Quick question – Could you write an update that would accommodate for the “slug name” as well as the ID? This is especially useful when building a theme on a dev server to be transferred live where IDs may change, but slugs stay the same.
Thank you, sir!
There is a more efficient way, yo have only to check the post_parent variable:
function is_subpage($the_id){
global $post;
if ($post->post_parent == '20') {
return true;
} else {
return false;
}
}
I just used:
function is_subpage($the_id){
global $post;
if ($post->post_parent == $the_id) {
return true;
} else {
return false;
}
}
if(is_subpage(11)) {
// do something
}
works like a charm!