Alex Barber | Digital Artist

time to push the pixels

Great Grand Parent ID’s in WordPress

http://andyweigel.com/blog/wordpress-how-tos/great-grand-parent-ids-in-wordpress/44

$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”;

simpler from a post comment:

function get_top_ancestor($id){
$current = get_post($id);
if(!$current->post_parent){
return $current->ID;
} else {
return get_top_ancestor($current->post_parent);
}
}

Next Post

Previous Post

© 2024 Alex Barber | Digital Artist