• Does anyone know how to modify or filter the body_class() output to get a category name or slug as a body class for single posts?

    Right now, for single posts it only outputs something like:
    <body class="single postid-481">

    Thanks,
    Michael

Viewing 11 replies - 1 through 11 (of 11 total)
  • <?php $class='';
    if(is_single()) {
    	$category = get_the_category();
    	$class .= $category[0]->slug;
    }?>
    <body id="top" <?php if (function_exists('body_class')) body_class($class ); ?>>
    Thread Starter mpmchugh

    (@mpmchugh)

    Thanks! That worked perfectly… Would you know how to modify to get the parent category slug as well?

    thanks esmi!

    This is great, however it only puts the category slug into the body class

    <body id="top" <?php if (function_exists('body_class')) body_class('category'.$class ); ?>>

    to so it inserts category-slug

    <body id=”top” <?php if (function_exists(‘body_class’)) body_class(‘category’.$class ); ?>>

    Should be:

    <body id=”top” <?php if (function_exists(‘body_class’)) body_class(‘category-‘.$class ); ?>>

    To get “category-“. Also this seems to only grab the first category that a post is in not any subsequent categories (likely not a problem for 90% of cases).

    What would make this script uber is altering it to also print the parent category if the posts category is a child. Then we are entering CSS heaven.

    I’m trying to get this to work by mushing various bits of php together (designer not a coder) and will post back when/if I get something usable.

    although this posts “category-” on it’s own when viewing a category. Not much of a problem appart from being a little inelegant.

    here is a function you can put in your functions.php to get the slug in the body class for singles.

    add_filter('body_class','add_category_to_single');
    function add_category_to_single($classes, $class) {
    	if (is_single() ) {
    		global $post;
    		foreach((get_the_category($post->ID)) as $category) {
    			echo $category->cat_name . ' ';
    			// add category slug to the $classes array
    			$classes[] = 'category-'.$category->slug;
    		}
    	}
    	// return the $classes array
    	return $classes;
    }

    eastwoodarts, that function works great! Thanks for posting. It’s doing something odd, though. It’s creating more parameters in the body tag.
    So, for example:
    I have a category called “DIY Projects” in the parent category “Projects”. Ten the body tags ends up looking something like this (misc. classes edited out):
    <body class="category-day-projects category-projects" projects="" projects="" diy="">
    See those weird extra pieces tagged on to the end?
    I noticed (because of a spelling error that I happened to have in one of my names) that it’s adding these from the category names, not the slugs.

    double posting: Oh, now I see it’s the “echo…” line adding that. Is there a reason you included the “echo category name” line? (I’m pretty new at this, so just trying to understand the reasoning behind it…)

    I’ve recently released a plugin that does everything requested in this thread (including parent categories). It is now available in the WordPress Plugin Directory.

    https://www.ads-software.com/extend/plugins/ambrosite-body-class-enhanced/

    does this work with 3.0? i can not get the name of the category to pull in to the body class.

    Tired the plugin but got errors..would prefer not to use one anyway.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘modify body_class() output to show category-{slug} for single posts’ is closed to new replies.