Get ID of parent category

The problem

Sometimes you need to get the ID of the parent category when you’re browsing a child category page. Don’t you? 🙂

The solution

Get the ID of the parent category with a few lines of PHP code:

<?php
if( is_category() ) {
  $q_cat = get_query_var('cat');
  $cat = get_category( $q_cat );
  echo $cat->category_parent; // Print the ID
}
?>

Requirements

  • WordPress 1.5.1 and above

Tags: , , ,

Leave a comment