Home

Skip to Content Skip to Navigation

Wordpress Plugin: Is Child Of

January 8th, 2009

What does it do?

Ever wanted to find out with PHP and Wordpress whether a page is a subpage of another? Fair enough, that is easy to do,  using $post->post_parent. But what if you want to find out if a page is a subpage of a page two or more levels above it? That's where my plugin comes in. It's a function that will keep jumping up levels of the page hiearchy from the page it is called on until 1) it reaches the top without encountering the page you are looking for, returning false or 2) reaches the page you are looking, and therefore it returns true.

How does it work?

It's pretty simple, and take's two inputs, the second one which is optional. The first input is the id of the top page you are trying to see this is a child page of.  For example, take a look at this page structure, then the code below:

  • Top Link One
  • Top Link Two
    • Sub2 Link One
      • Sub2.1 Link One
        • Sub2.1.1 Link One
      • Sub2.1 Link Two
      • Sub2.1 Link Three
    • Sub2 Link Two
    • Sub2 Link Three
  • Top Link Three
    • Sub3 Link One

// 'Top Link Two has the id 5

if(is_child_of(5))
{
// executes this code if you are on page or any child pages of 5
}

'Any child pages of 5′ would include all levels, even to the lowest level i.e. Sub2.1.1 Link One. Even on that, the above code will execute.

The other, optional input is actually the id of the page you are asking about. Now, normally, this is taken from the $post variable of the page that you are on by default, but if you any reason you want to know about a specific page, you put that page's id as the second parameter:


// Top page has to id of 5, and the page you want to know about has the id 10

if(is_child_of(5, 10))
{
// execute php code if the page with the id of 10 is a child page of 5
}

Why would you use it?

Well, this is up to you entirely, but what I used it for was to style a specific page and all it's numerous levels of subpages with a slightly different stylesheet. So I put this code in the header.php file of my template:


// the id of the top page is 5

if(is_page(5) || is_child_of(5))
{
// php code goes here to include the styleshet
}

So this statement will run if you are viewing a page with the id 5 or any of it's subpages on any number of levels.

Any questions or feedback, please do let me know :) So far I've only tested this for 2.7, but I'm pretty certain it should work for 2.5 as well. Any help on this would be greatly appreciated!

15 Responses to Wordpress Plugin: Is Child Of

Avatar

Raphael Hurwitz

January 8th, 2009 at 6:39 pm

I do not understand this as like maths, my brain has switched off to numbers and figures and technical talk, however you are a leg.

Avatar

WordPress Plugin Releases for 01/09 | Weblog Tools Collection

January 9th, 2009 at 4:05 pm

[...] Is Child Of [...]

Avatar

WordPress Plugin Releases for 01/09 | Tips Wordpress

January 9th, 2009 at 9:10 pm

[...] Is Child Of [...]

Avatar

WordPress Plugin Releases for 01/09 | Wordpress Blog NL

January 9th, 2009 at 11:09 pm

[...] Is Child Of [...]

Avatar

Links for 10th January 2009 | Velcro City Tourist Board

January 10th, 2009 at 8:00 pm

[...] Wordpress Plugin: Is Child Of [...]

Avatar

Plugins released on 9 Jan 2009

January 11th, 2009 at 1:55 pm

[...] Is Child Of [...]

Avatar

Red Root » Wordpress Plugin: Is Child Of : PluginZilla

January 15th, 2009 at 3:29 pm

[...] Here is a new post at Red Root » Wordpress Plugin: Is Child Of. [...]

Avatar

WordPress Plugin Releases for 01/09 | Blogging Money Step

January 26th, 2009 at 6:05 am

[...] Is Child Of [...]

Avatar

WordPress Plugin Releases for 01/09 | Perfect Blogging Tools

January 26th, 2009 at 6:10 am

[...] Is Child Of [...]

Avatar

zeniph

January 30th, 2009 at 7:48 am

so nice and simple it hurts!

makes all the wp_list_pages hacks I’ve been so happy with seem archaic

Avatar

Dukessa

September 15th, 2009 at 1:42 am

Nice idea but… it doesnt work for me.
Im using it on archive.php to add some code in case the category is child of a certain parent.
I use the correct function but nothing happens…
It’s like it completely ignores it, and displays instead the “else”.
Is it compatible with WP 2.8.4 or am I missing something?

Avatar

Luke

September 15th, 2009 at 9:32 am

I haven’t tested it with 2.8.4, I built this for 2.7 I assume looking at the date I posted it, so it could be that yeah. Is there a PHP error or just it seems to be returning false? Make post the code here that you’re using it in, I’ll see if I can discern anything.

Avatar

Nick Soper

October 1st, 2009 at 2:15 pm

Hi Luke,

This looks like a great plugin, good work!

I was wondering, would it be possible to do something like this for categories?

So display some code if you are in a child of category x?

Avatar

Andy Fitzpatrick

October 16th, 2009 at 2:08 pm

Love the simplicity of the idea to cure a complex problem will have to try and implement this on our site. Many thanks.

Avatar

matt dj Tolley

October 20th, 2009 at 11:33 am

look really good i will have a play and se if i can get this working tonight

thanks

Matt

Comment Form

Back to top