id eric
Member
Posted 3 years ago #
if(is_category('11') || cat_is_ancestor_of('11', $catid))
With the above statement, I am trying to display a certain layout for the '11' category and all of its child categories. When I am viewing the '11' category, it displays the code within the if statement. However, when viewing any child of '11' - nothing is displayed.
What am I doing wrong?
id eric
Member
Posted 3 years ago #
I think I am missing the way I get the $catid. So here it is:
$catid = get_query_var('cat');
if(is_category('11') || cat_is_ancestor_of('11', $catid))
Again, what am I doing wrong?
id eric
Member
Posted 3 years ago #
I adjusted my code to the following:
$cat = get_query_var('cat');
$args = array(
'include' => $cat,
'hide_empty' => 0
);
$categories = get_categories($args);
if( ($cat == 11) || ($categories[0]->category_parent == 11) || ($categories[0]->category_parent == 28) || ($categories[0]->category_parent == 27) || ($categories[0]->category_parent == 26) ){
The downfall to this, is that it only deals with direct childs. So if you have 2 levels of categories, then you will have to manually add category_parent parameters. Is there a way to check if a category is a sub-category of a category that is higher up the chain? Maybe using the cat_is_ancestor_of function? I couldn't get it to work...
This works for me:
if ( cat_is_ancestor_of(6,20) ) {
echo '<p>Category 6 is an ancestor of category 20 </p>';
} else {
echo '<p>Category 6 is NOT an ancestor of category 20 </p>';
}
id eric
Member
Posted 3 years ago #
I guess my problem is determining the category id of the category that the user is currently viewing.
How would I adjust the cat_is_ancestor_of parameters to include the ID of the category the user is current viewing?
thanks in advance.
In a category template?
$catid = get_query_var('cat');
id eric
Member
Posted 3 years ago #