
Have you ever wanted to know if the page you’re on is a child of a certain page because you needed to know that information in order to add a snippet of code to the sidebar or remove something or whatever you had planned but you tried and just couldn’t figure out a solution that was simple and easy to maintain? Well, here is a piece of code that will do just that. I use this very code all over the place, on pretty much every project I do. The concept is simple. I need to know more than if the is_page(‘portfolio’); I need to know if this is_page_or_sub(‘portfolio’);
This is pretty much the same function as is_page(), it lets you pass in a page ID or slug, your choice, but the only real difference is that it goes beyond a single page and looks for a family relationship. The function will return true for all children and grandchildren of the page id or slug you pass in. For example, you want to show two different sidebars, one for normal content and one for the portfolio section. You would use this function to do that. First, copy and paste this code into your functions.php and then take a look at the usage example below.
EDIT:
You can get an updated version if this function and as a plugin if you got to here.
The Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //If is page or subpage of $my_page, works with both ID or name. if (!function_exists('is_page_or_sub')) { function is_page_or_sub($my_page) { global $post, $wpdb; $grand_parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$post->post_parent."' AND post_type = 'page'"); // If you pass in a string, get the page ID of $my_page to use below if (is_numeric($my_page)==false) { $my_page = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$my_page."' AND post_type = 'page'"); } // If this is $my_page or the child or grandchild of $my_page return true if ( is_page($my_page) || $post->post_parent == $my_page || $grand_parent == $my_page ) { return true; } // Else return false return false; } } |
Sample Usage
1 2 3 4 5 | if (is_page_or_sub('portfolio')) { DISPLAY PORTFOLIO SIDEBAR } else { DISPLAY REGULAR SIDEBAR } |
If you have any question feel free to comment below. Have fun taking over the internet one WordPress blog at a time.
Donations
- Make a donation and help keep the awesomeness flowing.
Disclaimer
Valen Designs grants you a nonexclusive copyright license to use all programming code examples from which you can generate similar function tailored to your own specific needs.
Valen Designs, cannot guarantee or imply reliability, serviceability, or function of these programs.
All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.






Comments
This is exactly what I was looking for! Thank you so very much for this piece of code, you saved my sanity. I have been trying to figure out an elegant way to do this but never found a solution.
PERFECT!
Thanks, and it’s too bad you couldn’t find a solution quicker cause there are other ways of doing this, but I like this way cause you can pass in a page name or ID, so you are not limited to just an ID and forced to go look for it. It’s mostly a convenience thing.
I have another one that I use for categories that I will post in a few days that test if is category or subcategory of the passed in value either name or ID it will return true. I use it heavily on all the Tuts+ websites to build the category navigation.
Thanks, this is really helpful!
Here is my version which loops through the tree till it finds a match or gives up, it also lets you compare pages, useful for use in other functions!
if (!function_exists(‘is_page_or_subpage’)) {
function is_page_or_subpage($thePage, $compare = null)
{
global $post, $wpdb;
if (!$compare) {
$compare = $post->ID;
}
if ($compare == $thePage) {
return true;
} else {
$currentPage = $compare;
$parent = 1;
while ($parent != 0) {
$parent = $wpdb->get_var(“SELECT post_parent FROM $wpdb->posts WHERE ID = ‘”.$currentPage.”‘”);
if ($parent == $thePage) {
return true;
} else {
$currentPage = $parent;
}
}
}
}
}
I need to pass the page parent id to the body id-tag in single.php.
This is how I go about it (without success):
I have index.php and 3 other pages I publish to. One of them is “inspiration.php”
Inspiration.php starts with calling
“include (TEMPLATEPATH . ‘/start_inspiration.php’);”
Then it calls header.php and so on. All this works fine.
In start_inspiration.php (there is a separate start_xxx.php for each page template) the is given an ID. I use this id to decide what menu element is current and style it.
However I cant get your function to work. If I feed it a string it always return the first if-statement as true. If I feed it a number it always returns false. (That way I can see that the code executes)
My code looks like this:
<body id=”">
I am neither at wp or php pro so any help would really be appreciated.
I’m having a tough time picturing what exactly you’re trying to accomplish. When you’re in the single.php are you in a category. Are the pages that you submit to parent categories that list all the posts for that category?
It sounds more like you need to test if you are in a certain child category of a parent category, yes?
Sorry for being too short.
Each of my four pages have a visual tab that I style. I use a match between body id and the classes in the menu. When they match the css changes background to make the tab look current.
Each tab presents 3 articles/posts, cut short by the more-tag. When I click Read More the single.php is used to present the full article/post.
I want to keep the current styling of the tab that was active when I clicked on Read More.
I am trying to solve this by passing some kind of id representing the page where the post started and use it to set the body-id in the single.php.
I think you might be better suited using the function I just posted about called is_categroy_or_sub(), let me know if that works out for you.
Thanks. I’ll try that.
Glad to help.
Hi again.
It works fine if I test for one category. I have between 4-6 categories on each page and I would like to test for all of them in one statement. ( If this post belongs to cat 13 or 14 or…or 17, then return true)
I tried in a few different ways without success. Is it possible?
I really appreciate you taking the time!!!
Moving conversation to other post.
The best information i have found exactly here. Keep going Thank you
Thanks, this post was really helpful! Keep up the good work!
It is remarkable, this valuable message
Hi,
This works great for me, but only on parents and children..
When I add grandchildren nothing shows up!
I am using it to put a nav in the sidebar of a parent page and all of its offspring… but as I said, when it comes to the grandchildren, I get nothing.
Why can this be?
Thanks!
did you try this http://valendesigns.com/news/added-plugin-to-wordpress-directory/
Awesome! Worked great!
Glad to hear the updated function worked out for you. The one here is about a year older than the one I submitted to WordPress as a plugin so it may or may not be the fact that it was made when 2.6 was just coming out.