• Resolved zipiz

    (@zipiz)


    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter zipiz

    (@zipiz)

    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’;

    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;
    }
    Thread Starter zipiz

    (@zipiz)

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

    Thread Starter zipiz

    (@zipiz)

    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.

    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');
    Thread Starter zipiz

    (@zipiz)

    I owe you a beer. Big thanks ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add NAME of top-level category to body class’ is closed to new replies.