WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add NAME of top-level category to body class (8 posts)

  1. zipiz
    Member
    Posted 1 year ago #

    I tried several days to accomplish this, tried several code snippets, aparent solutions, but none of them accomplished that what I want.

    I simply want to remove all body classes and only add the top-level categry name (not ID!) to the body class.
    These are my categories:

    Apples
    --Juice
    --Cake

    "Apples" is the top-level category, "Juice" and "Cake" are both subcategories. So what I want is to add the class "apples" to the body on

    category/apples
    category/apples/juice
    category/aples/cake

    and all articles inside these categories.

    A simply solution must exist.

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    general:

    http://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters

    is that for category archives and/or single posts?
    what if a single post has several categories?

    possible useful functions:
    http://codex.wordpress.org/Function_Reference/get_the_category
    http://codex.wordpress.org/Function_Reference/get_ancestors

  3. zipiz
    Member
    Posted 1 year ago #

    I'm not a coder so all I can do is try to understand the code and copy/paste code others provide.

    The first link shows how to add the actual category name in the body, but not the top-level category (parent/root category).

    From the second link I tried several code snippets and again, they do not display the parent/root category in the body class.

    From the third link I do not quite understand how to make a code snippet off the information there to display the parent/root category in the body class.

    is that for category archives and/or single posts?

    For single posts and category archives.

    what if a single post has several categories?

    You make a valid point there. I totally forgot about the case of multi categories. In this case, I simply want to add a generic class in the body tag. I believe this should be doable with a conditional. Something like

    if (categories more than 1)
    echo 'genericClass';

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    this example will only affect the body_class in single posts and category archives;

    // Add specific CSS class by filter
    add_filter('body_class','top_cat_body_class');
    
    function top_cat_body_class($classes) {
    if( is_single() ) :
    	global $post;
    	$cats = get_the_category( $post->ID );
    	if( count( $cats ) > 1 ) {
    		return array('genericClass');
    	}
    	else {
    		$cat_anc = get_ancestors( $cats[0]->term_id, 'category' );
    		$top_cat = array_merge( array($cats[0]->term_id), $cat_anc );
    		$top_cat = array_pop( $top_cat );
    		return array(get_category($top_cat)->slug);
    	}
    elseif( is_category() ) :
    	$cat_anc = get_ancestors( get_query_var('cat'), 'category' );
    	$top_cat = array_merge( array(get_query_var('cat')), $cat_anc );
    	$top_cat = array_pop( $top_cat );
    	return array(get_category($top_cat)->slug);
    else :
    	return $classes;
    endif;
    }
  5. zipiz
    Member
    Posted 1 year ago #

    You are awesome! It works brilliantly. Thank you so much!

  6. zipiz
    Member
    Posted 1 year ago #

    It's me again. Can you please modify this code above to

    a) remove all classes from the body tag except the ones generated by the code

    b) add the same genericClass to all pages that are not in a category (tag pages, author pages, search pages etc.)

    I dying to figure it out. Your help is much appreciated.

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    the third-last line returns all the body_classes which are not affected by the category code;

    therefore change:

    return $classes;

    to:

    return array('genericClass');
  8. zipiz
    Member
    Posted 1 year ago #

    I owe you a beer. Big thanks :)

Topic Closed

This topic has been closed to new replies.

About this Topic