1

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,

1 Answer 1

1

You could use get_ancestors(). You can transform this:

<a href="#" class="<?php $category = get_category($cat);
echo $category->category_nicename;?>"><?php single_cat_title('') ?></a> 

Into this (I don't know what $cat is as you have not shown it in your question, I assume it is a category ID, if not, you have to provide category ID):

<?php

$cat_ancestors = get_ancestors( $cat, 'category' );
$top_parent = get_category( end( $cat_ancestors ) );

?>

<a href="#" class="<?php esc_attr_e( $top_parent->slug );?>"><?php echo $top_parent->name; ?></a> 

To make the code works also if the category is already the top parent category, you need to check the value returned by get_ancestors. For example:

<?php

$cat_ancestors = get_ancestors( $cat, 'category' );

if( !empty($cat_ancestors) ) {

    $cat = get_category( end( $cat_ancestors ) );

} else {

    $cat = get_category( $cat );

}

?>

<a href="#" class="<?php esc_attr_e( $cat->slug );?>"><?php echo $cat->name; ?></a>
4
  • Hi, thank you; so I've put this instead:<?php $cat_ancestors = get_ancestors( $cat, '36 ' ); $top_parent = get_category( end( $cat_ancestors ) ); ?> <a href="#" class="<?php echo $top_parent->slug;?>"><?php echo $top_parent->name; ?></a> BUt it doenst work at all .. doesnt display the category_name . .. nothing :(
    – tibewww
    Commented Feb 23, 2015 at 11:20
  • Of coure it doesn't work. get_ancestors( $cat, '36' ); it is not valid, the second parameter is the object type, in this case 'category' (name of the taxonomy used). In the first parameter is where you have to provide the ID. If the category ID is 36: get_ancestors( 36, 'category' );. Please, refer to the get_ancerstors documentation linked in the answer.
    – cybmeta
    Commented Feb 23, 2015 at 12:02
  • HI back, thank you for your time, WHen i provide the id; I put the following code: <?php $cat_ancestors = get_ancestors( 32, 'category' ); $top_parent = get_category( end( $cat_ancestors ) ); ?> <a href="#" class="<?php echo $top_parent->slug;?>"><?php echo $top_parent->name; ?></a> I'm on my category page (ID 32) - but nothing happen - in the html I can see that not class is given to it . . . :(
    – tibewww
    Commented Feb 23, 2015 at 12:21
  • Maybe the category 32 has not ancestors and it is the top parent cagegory????. I've updated the answer with code that works for categories with ancestors and with categories that are actually the top parent category. It is tested and working; if it doesn't work for you, please, provide some debug information.
    – cybmeta
    Commented Feb 23, 2015 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.