Have you ever needed to find all the ancestor ID’s of a page in WordPress? If so, you’ve come to the right place. Here’s some quick code to give you up to the great grand parent ID.
$current = $post->ID; $parent = $post->post_parent; $get_grandparent = get_post($parent); $grandparent = $get_grandparent->post_parent; $get_greatgrandparent = get_post($grandparent); $greatgrandparent = $get_greatgrandparent->post_parent; echo "parent = $parent; grandparent = $grandparent; greatgrandparent = $greatgrandparent";
If you wanted to, you could continue this pattern to N number of ancestors…
Hopefully you’ve found this quick tutorial valuable. Contact me if you have any questions or would do it a different way. There’s 1,001 ways to do everything – that’s why I love coding.
Posted on July 10, 2009 in Wordpress How-To's and has been tagged as wordpress.
9 Comments
My name is Andy Weigel and I'm a web developer and designer in Pittsburgh, PA.
I focus on combining design with technology to build compelling, creative, easy-to-navigate web sites and custom web applications for organizations and businesses of all shapes and sizes. My specialty is WordPress. And most of all, I love what I do!
Hey Andy, I’m not sure if you’re familiar with recursion, but just in case there is someone else that comes across this who would like to grab a top element, here’s a function I wrote after discovering that get_post_ancestors only recursed one parent (thus rendering it useless for my needs):
function get_top_ancestor($id){
$current = get_post($id);
if(!$current->post_parent){
return $current->ID;
} else {
return get_top_ancestor($current->post_parent);
}
}
this function will always return the parent, grandparent, or higher, closest to the top. Very good for submenus.
Comment by Jesse — January 19, 2010 @ 9:08 am
Awesome function, Jesse. I’ll make sure I add that to my code snippets. Thanks for sharing!
Comment by Andy — January 19, 2010 @ 6:54 pm
Can I use the recursive script to get a page slug rather than the ID? This would be more useful when applying CSS.
eg: Let’s say I have two section on my site: About and Products.
About
–location
–number of staff
–hours
Products
–muffins
— blueberry
— bran
–belly button lint collector
–1.5″floppy disks
I can add the following function to my function page:
ID, ARRAY_A);
$slug = $post_data['post_name'];
return $slug; }
?>
and this line to my header.php:
<body class="”>
Now my page is wrapped in the slug name for the page. This is useful as a sort of ‘if” statement for CSS.
// If this is about page: the header background is blue
// If this is products page: the header background is red
.about .header{background-colour: blue;}
.about .header{background-colour: red;}
This can be done with the ID as you have it but it’s not as easy to read:
.about .10{background-color: blue;}
The problem with this ‘slug’ function is when I get to a child, grandChild or deeper. The slug returns the name of that page. : “blueberry” or “bran” and not Products, rendering the CSS background colour useless.
Thanks in advance for your thoughts.
Comment by frank — March 1, 2010 @ 2:10 pm
I came up with a slight variation that outputs the parent or page title.
//get the root parent page title
function get_top_ancestor($id){
$current = get_post($id);
if(!$current->post_parent){
return $current->post_title;
} else {
return get_top_ancestor($current->post_parent);
}
}
SO, if you put teh above script into your function.php and the following into your body tag like this:
<body class="”>
Your CSS can control the following tag:
//the header background for about and it’s children are now blue
.about .header{background-colour: blue;}
or change all the text links in the about section to orange(why… why not)
//all text on the about page and it’s children are orange
.about a{color: orange;}
enjoy : )
Comment by frank — March 2, 2010 @ 2:44 pm
Thanks a lot! this is really helpfull!!
Comment by @shimapi — May 17, 2010 @ 12:56 pm
Thanks! vary handy code. Also like the recessive process.
Comment by Sisir — October 11, 2010 @ 6:20 am
Hey , thanks for sharing.. im getting the idea I really going to need this. Just tell me what it does? lol.. im not quite sure what the code actually does when activated. Yes, Im new. Why would i need to find a grandparent? Would appreciate an explanation, cheers.
Comment by Rams — November 16, 2010 @ 12:46 am
Thanks for sharing. I’ve been looking for a clean way to express this. I wish I’d found this three or even four WordPress projects back.
Comment by Loren Helgeson — February 16, 2011 @ 7:05 pm
You can use the built in wordpress function get_post_ancestors too. This returns an array of the ID numbers of all of the posts who are ancestors to your current post. An example of use would be:
if (!$post->post_parent){
echo ‘We are not a child’;
} else {
$ancestors = get_post_ancestors($post);
foreach ($ancestors as $anc){
echo ‘Found an ancestor ID ‘.$anc;
echo ‘The post title is ‘.get_the_title($anc);
}
}
Hope that helps to shed some light…
Tim.
Comment by MadeGlobal — April 11, 2011 @ 3:00 pm
RSS feed for comments on this post. TrackBack URL
Leave a comment