I need to grab the sub category. How can I do that?
NB
<?php $cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);
echo $parentCatName; ?>
gets me the wrong ancestor, but not the parent
I need to grab the sub category. How can I do that?
NB
<?php $cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);
echo $parentCatName; ?>
gets me the wrong ancestor, but not the parent
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
Does get me a sub category, but the sub category of the wrong parent category..
<?php $cat = get_the_category(); $cat = $cat[0];
echo(get_category_parents($cat, TRUE, ' » ')); ?>
Gets me two categories of which one is the wrong parent category and the other a sub category of that same parent..
Well, got a little further:
<?php $cat = get_the_category();
$parentCatName = get_cat_name($cat[2]->parent);
echo $parentCatName; ?>
gets me the right parent category, but I need to display $cat[1] when on a sub category of that page. How to do that?
Maybe
<?php echo get_category_parents( get_query_var('cat') , true , '' ); ?>
could work. Just needs a little more work..
<?php
foreach (get_the_category() as $cat) {
$parent = get_category($cat->category_parent);
$parent_name = $parent->cat_name;
$parent_url = $parent->slug;
}
?>
<?php echo $parent_name; ?>
Shows wrong parent again and does not show the sub category or category child. Maybe I should just get the category..
Got it.
<?php
foreach((get_the_category( get_the_ID() )) as $category)
{
if(cat_is_ancestor_of(get_cat_ID('x') , $category->cat_ID) )
{
$category_link = get_category_link( $category->cat_ID );
$category_name = get_cat_name( $category->cat_ID );
$Finalcategory = $category;
break;
}
}
?>This topic has been closed to new replies.