This is my cat directory structure

  • Games

    • Cricket -- Bat -- Ball -- Wicket
  • Machines

    • Electrical -- Fan -- generator -- Computer

I am on category page of Bat or Ball or Wicket. Can anyone tell me how i can get its parent id like id of cricket not Games. I want to get the category id of Cricket.


The function get_ancestors() will give you an array containing all of the parents of your category with the nearest ancestor being first in the array. So:

$cid = 5; // your category ID
$c = get_ancestors($cid,'category');
var_dump($c); // debugging
$p = array_shift($c);
// var_dump($p); // debugging

Use $p to do whatever you need to do.

| improve this answer | |

you can do like this

$term = get_queried_object();   
$parent= $term->parent;

echo $parent;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.