
I recently added an updated version of the is_page_or_sub() function now called is_page_or_ancestor() to the plugins directory over at WordPress.org. The new version of the function is a recursive test to see if the page ID or name you pass into the function is actually the current page or one of its ancestors. Basically you can test to see if you are viewing an ancestor of the About page and if so execute some code (i.e. change a pages class or id).
I use this function on my site and pretty much on every other site I’ve ever built. However, the only different is this new code is leaner and will test many levels deep. A very handy tool for the custom type themes that seem to be popping up in the wake of what I am calling a WordPress feeding frenzy. The popularity of this platform has been growing exponentially and who can blame it, I absolutely love working with WordPress.
Anyhow, this is the first plugin I have submitted to be hosted in the Directory, mostly I just wanted to test out committing a plugin to the directory and this seemed like a logical place to start. If you’re interested in what was changed here is the code. Also, you can update the old functions code with this much better code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * The parameter can contain the page ID, or page name * * @since 1.0 * @uses $post * @uses $wpdb * * @param mixed $page either int or string * @return bool */ if (!function_exists('is_page_or_ancestor')) { function is_page_or_ancestor($page = '') { global $post, $wpdb; // If is not numeric get page ID if (!is_numeric($page)) { $page = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page."' AND post_type = 'page'"); } // Recursive search through page hierarchy if ( is_page($page) || ( is_array($post->ancestors) && in_array($page, $post->ancestors) ) ) { return true; } return false; } } |
Sample Usage
1 2 3 4 5 | if (is_page_or_ancestor('about')) { DISPLAY SOMETHING } else { DISPLAY SOMETHING DIFFERENT } |
Download File
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
Cool, this looks very useful indeed, thank you for making it into a plugin :)
Are the PHP warnings at the top of this post supposed to be there? Looks like you’re executing PHP directly in your post and it’s failing on preg_match().
I just upgraded the new syntax highlighter and now it’s doing this. Weird! It was fine when I left an hour ago. I’ll look into it.
I had to revert back to an older version 0.9.4 of the plugin wp-syntax cause the latest 0.9.6 just tried to make me look like an idiot. What a let down. thanks for the heads up. I was out getting new tires and got the email. It was a bit frustrating not being able to fix it from my iPhone LOL, but thanks.
Hey Derek… So here’s my situation. I absolutely love the way that the Valen Designs Vesper theme handles the Page/Sub-page sidebar listing. However, for various reasons, I’m not able to use the Vesper theme in a project that I’m currently working on. So my question is this: is this plugin that you’ve created supposed to provide non Vesper themes with the kind of Page/Sub-page sidebar functionality included in Vesper? If so, does the plugin just automatically set things up? Or do I need to do some tweaking in order to get the functionality working?
I’m kind of new to WP, so I’m not really sure if I have all of the WP terminology correct. Anyway, in my best *Wordpress lingo*, the gist of what I want to learn how to do do is the following.
How can I set things up so that when I’m on any *sub-page* of *page* X, there is a list of all other *sub-pages* of *page* X listed in the side-bar of the *sub-page* that I’m on. Did that make sense? I hope so.
For instance, here’s a link to a testsite that I’m building.
http://www.testsite.tennberg.com/iep/
Right now, I have two *pages* (called A and B) and one subpage for each of the two pages (Augustine as a subpage of A, and Big Bang Theory as a subpage of B). And here’s what I want to do. When someone is viewing the Augstine subpage of A (or any other subpage of A), I would like all and only subpages of A to be listed in the sidebar. I’d also need this to work for all other page / subpage relations (e.g. when I create a B page and various subpages of B, the same sidebar sorting takes place, except this time for the B subpages).
As mentioned above, this sort of sorting functionality seems to be automatic in Vesper, but sadly I can’t use that theme for this project. Anyway, I could really use some help with this.
Thanks…
If you go into the sidebar.php in vesper there is some code used to do that that you could copy and paste and it should work fine, you don’t really need this plugin to do that.
There are a lot of ways to do what you’re asking and I would do it differently than the vesper theme does because I can write more complex WP code than what I wrote a year ago, but for your purposes that snippet should work. If not let me know and I’ll try and help you further.
I am using this in my side bar and have also enabled the search widget. When I search for something, the search page comes up with this error:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/user/domain.com/wp-content/themes/newkubrick/functions.php on line 43
Any idea why that might be? It occurs only in the search page.
How are you using the plugin and are you sure the plugin is the cause?
Great plugin! Unfortunately I’m also getting this error: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /wp-content/plugins/is-page-or-ancestor/is_page_or_ancestor.php on line 37
Using Is Page or Ancestor v1.0.1 and WordPress 2.9.1.
This error is happening when using the plugin code in a category template. It seems to break because the category template is not a page.
I have conditional statements in the sidebar template saying “if is_page_or_ancestor do this, if is_category do that”.
Any suggestions? Thanks!
Try this. It’s not tested, let me know if it works though.
if (!function_exists(‘is_page_or_ancestor’)) {
function is_page_or_ancestor($page = ”) {
global $post, $wpdb;
// If is not numeric get page ID
if (!is_numeric($page)) {
$page = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_name = ‘”.$page.”‘ AND post_type = ‘page’”);
}
$ancestors = get_post_ancestors($page);
// Recursive search through page hierarchy
if (is_page($page) || in_array($page, $ancestors)) {
return true;
}
return false;
}
}
Hi Derek,
I had the same error as Mike, using the same configuration except WP 2.9.2 and your revised code works perfectly! Hopefully you can update the plugin soon in the WP repository because this is essential code for anyone hard coding their navigation. Thanks for making this, saved my bacon for sure!
Trevor
Shoot, actually no you fix does not work for me. The error is gone but so is the current page class. Darn.
try:
function is_page_or_ancestor($page = ”)
{
global $post, $wpdb;
// If is not numeric get page ID
if (!is_numeric($page)) {
$page = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_name = ‘”.$page.”‘ AND post_type = ‘page’”);
}
// Recursive search through page hierarchy
if (is_page($page) || in_array($page, $post->ancestors)) {
return true;
}
return false;
}
Hi Derek – thanks for this and the related category function (is_category_or_sub)… both huge help for me as I build out a theme.
I am having the same problem as a few other commenters posted above – getting the following error message:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/username/example.com/wp-content/themes/theme-name/functions.php on line 120
Line 120 is from your function (the latest version you’ve commented above… though I get the same error with the previous version as well):
Line 120 –> if (is_page($page) || in_array($page, $post->ancestors)) {
I am using the function call in my nav menu, pretty much as you would expect. Any idea what about that “in_array” call is off?
Perhaps the plugin version has been corrected? I’ll try that. Otherwise, I suppose I’ll use the older / original version of this function, is_page_or_sub. But I do hope you can fix the function.php version, as I prefer that method rather than using plugins.
Thanks!
Change to this below, I ran into the same issue, and it’s because the page you’re testing doesn’t have any ancestors.
// Recursive search through page hierarchy
if (is_page($page) || (is_array($post->ancestors) && in_array($page, $post->ancestors) )) {
Yeah! That was it, works perfectly now. Thanks again.
Glad to help, I needed to update that plugin anyhow. Thanks for the donation, as well.
Updated the plugin in the WordPress directory.
donation on the way – keep the awesomeness flowing ;)