Get Top Parent or Grandparent ID Wordpress

/ Published in: PHP
Save to your folder(s)

From 'Andy' in the comments.

Copy this code and paste it in your HTML
  1. function get_top_ancestor($id){
  2. $current = get_post($id);
  3. if(!$current->post_parent){
  4. return $current->ID;
  5. } else {
  6. return get_top_ancestor($current->post_parent);
  7. }
  8. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.