Im having some trouble since a week now . .. I'm having a bad time with that !
I explain my situation:
I have setup a style for post category.
(a background-color and a small image background).
eg.
Parent A -Child 1 -Child 2 -Child 3
Parent B -Child 1 -Child 2
Parent C -Child 1 -Child 2
The loop i am using is:
<a href="#" class="<?php $category = get_category($cat);
echo $category->category_nicename;?>"><?php single_cat_title('') ?></a>
This works fine when I'm on a parent_category page, it bring the style I have set up by css, using the category_slug as a class.
(eg of css for on category:)
article.category-business-and-finance .meta-category2 a.business-and-finance {
color: white;
float: left;
padding: 2px;
padding-left: 25px;
background: url(myimg) no-repeat left ;
background-size: 15px 15px;
background-color: #c3bc19 ;
background-position-x: 5px;
padding-right: 5px;
}
Howver, when I go to the category_page of a child_category, it show up the style of a different category. as some post are set up to multipel parent_category and multiple chld-category as the same time. I don want to create a css for each child_category, as lot of sub category would be added in the futur.
That I d like to achieve, is for the child_category, to bring the class of it's current parent_category slug.
How is this possible to achieve ?
I've tried this loop instead:
<a href="#" class="<?php $parentscategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent == 0) {
$parentscategory .= ' ' . $category->slug . ' ';
}
}
echo substr($parentscategory,0,-2);?>"><?php single_cat_title('') ?></a>
This add the class from all the parent_category attach to the post . .. Is there any way to make it only to the current one ?
( eg. im on the category page of Child-1 from Parent A, but the post is also attach in Parent B and Parent C. it bring me the class of Parent A B and C and the moment - I would like to attach the class of only Parent A - as im on child category of Parent A page).
How can i make this ? should i try a current_parent_category ? cat_ancestor ?
Thank you for all your time,