parent category id from category id – wordpress

Define a function returns the current category id

function getCurrentCatID(){
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var(‘cat’);
}
return $cat_ID;
}

Call the function “get_category() ” to fetch the current category details

$category = get_category( getCurrentCatID() );//
$parentID = $cat->category_parent;

echo $parentID;

Leave a Reply

Your email address will not be published. Required fields are marked *