olddocks
Member
Posted 3 years ago #
Lets say i am creating 3 parent categories and 10 sub categories to each parent. My wordpress post belongs to one sub category of a particular parent category
How do i get the parent category name ONLY? i dont want subcategories names? what wordpress code would do that?
spade5702
Member
Posted 2 years ago #
I would like to know how the get the category parent name too. Can anyone please help?!!
First, get the parent's ID then the name... Something like the one below, but you should adjust it to your needs. The example below is good if you only have one sub category.
<?php
foreach((get_the_category()) as $childcat) {
$parentcat = $childcat->category_parent;
echo get_cat_name($parentcat);
}
?>
If you wanted to get the parent category for the category currently being queried, you could use.
<?php echo get_category_parents( get_query_var('cat') , true , '' ); ?>
It should show each parent.. supports a seperator to.
Set true to false to not display as a links..
More info (though somewhat vague).
http://codex.wordpress.org/Function_Reference/get_category_parents