Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add the parent category class in post_class() of some posts from one of my subcatgories. I have an advanved theme so I need to style all the posts from the parent cateogry using it's class, not the subcategory class (too much hassle in CSS).

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Perhaps something like:

function mytheme_get_post_class_parent_cats() {
    $parent_cat_classes = array();
    // Get parent categories
    $post_cats = wp_get_post_categories();
    // Loop through post categories
    foreach ( $post_cats as $post_cat ) {
        $cat_parents = get_category_parents( $post_cat , false, ',', true );
        // if there are any parent categories
        if ( $cat_parents ) {
            // create an array
            $cat_array = explode( $cat_parents, ',' );
            // First key of the array is the top-level category parent
            $parent_cat_id = $cat_array[0];
            // Get the name of the top-level parent category
            $parent_cat = get_cat_name( $parent_cat_id );
            // create the CSS class .category-parent-{categoryname}
            $parent_cat_classes[] = 'category-parent-' . $parent_cat;
        } else {
            // Otherwise, the category is a top-level cat, so use it
            $cat_name = get_cat_name( $post_cat );
            $parent_cat_classes[] = 'category-parent-' . $cat_name;
        }
    }
    // Return CSS class, or false
    return $parent_cat_classes;
}

Then, when you call post_class():

<?php post_class( mytheme_get_post_class_parent_cats() ); ?>
share|improve this answer
    
There seems to be a problem with your function, it's not working o_O –  rats Jul 17 '11 at 21:19
    
It's example code for implementing the two referenced functions. Can you provide more detail? What's not working. –  Chip Bennett Jul 17 '11 at 21:22
    
as this is for a post, the function should loop through the post's categories: codex.wordpress.org/Function_Reference/wp_get_post_categories –  Michael Jul 17 '11 at 21:30
    
Okay, see updates. Added parameters to get_category_parents(), and added an explode() call to create the array from which to get the top-level parent category. –  Chip Bennett Jul 17 '11 at 21:31
    
Oh, good call, @michael! Let me adjust things a bit... –  Chip Bennett Jul 17 '11 at 21:34
show 4 more comments

A friend found the perfect solution, so I think it's fair to share it with you guys:

function rats_class($classes){
 global $post;
 $cat=get_the_category(get_query_var('post'));
 if(is_array($cat)&&!empty($cat)){
  if($cat[0]->category_parent!=0){
   $parents=get_category_parents($cat[0]->term_id,false,'@',true);
   if(!empty($parents)){
    $parent=explode('@',$parents);
    $classes[]='category-'.$parent[0];
   }
  }
 }
 return $classes;
}
add_filter('post_class','rats_class');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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