Welcome, guest!

Feel free to read the blog, browse for themes, or join the club.

Is it possible to add the name of the parent category to the body class?

  1. Hello,

    Is there anyway to add the name of the parent category to the body class? At the moment, in 0.5, a single post has the following classes on the body:

    <body class="ltr logged-in archive category category-24-hours windows firefox firefox-3 primary-active secondary-inactive subsidiary-inactive">

    Where "category-24-hours" is the name of the category that the post belongs to. I would also like to have the parent category in the body class, e.g. something like "parent-category-features" , where "features" is the parent category of "24-hours". Is this possible to achieve?

    Thanks for Hybrid btw - I've only just started working with it, but I'm loving it so far!

    Many thanks in advance.

    Cheers,
    Richard

  2. You must be a logged-in exclusive member to view this reply.

  3. Justin, did you ever find a way to do this?

  4. You must be a logged-in exclusive member to view this reply.

  5. That added the parent category class to single posts, but how can I also add it to a subcategory page?

  6. You must be a logged-in exclusive member to view this reply.

  7. Absolutely perfect! Thanks again Justin.

  8. This works fine, but if one post is only in one category how add category-xxx style to body class?

  9. add_filter( 'hybrid_body_class', 'my_body_class' );						// Custom body class
    
    function my_body_class( $class ) {
    	global $wp_query;
    	if ( is_single() ) {
    		$terms = get_the_terms( $wp_query->post->ID, 'category' );
    		foreach ( $terms as $term ) {
    			$category = get_category( $term );
    			$class .= " category-{$category->slug}";
    			}
    		}
    	return $class;
    }
  10. Thanks a lot depx!

    Finally I combined this function with http://themehybrid.com/support/topic/galleries-different-styles-by-categories and works fine to categories and first subcategories with posts and attachments but if one subcategory has other subcategory then doesn't work :-(

    This is the code

    add_filter( 'hybrid_body_class', 'my_body_class' );
    
    function my_body_class( $class ) {
       global $wp_query;
    
       if ( is_single() && !is_attachment() ) {
          $terms = get_the_terms( $wp_query->post->ID, 'category' );
          if ( is_array( $terms ) ) {
             foreach ( $terms as $term ) {
                if ( $term->parent == 0 ) {
                   $category = get_category( $term );
                } else {
                   $category = get_category( $term->parent );
                }
             $class .= " category-{$category->slug}";
             }
          }
       }
    
       if ( is_attachment() ) {
          $terms = get_the_terms( $wp_query->post->post_parent, 'category' );
          if ( is_array( $terms ) ) {
             foreach ( $terms as $term )
                $class .= " category-{$term->slug}";
          }
       }
    
       if ( is_category() ) {
          $term = $wp_query->get_queried_object();
          if ( $term->parent ) {
             $parent = get_category( $term->parent );
             $class .= " category-{$parent->slug}";
          }
       }
       return $class;
    }

    How can I fix it?

    Thanks!

  11. You must be a logged-in exclusive member to view this reply.

  12. I'm asking how add the parent category of child-child category to the boy class.

  13. You must be a logged-in exclusive member to view this reply.

  14. I've this:

    Category(1) --> subcategory (1.1) --> subcategory (1.1.1) --> posts

    When I have post s under subcategory (1.1.1) need add the parent category (1) to the body class.

  15. You must be a logged-in exclusive member to view this reply.

Reply

You must log in to post.

Limited Access

If you have an account, please take a moment to log in.

Non-exclusive members have limited access to the support forums.

To enjoy the full range of support, sign up for an exclusive membership in the theme club.

Support Forums

  • Bliss Theme (253 posts)
  • Hybrid Theme (21,735 posts)
  • Options Theme (10,356 posts)
  • Structure Theme (2,747 posts)
  • Outline Theme (36 posts)
  • Visionary Theme (767 posts)
  • bbPress Themes (305 posts)
  • WordPress Plugins (2,045 posts)
  • General Discussion (5,276 posts)