WordPress.org

Ready to get started?Download WordPress

Forums

modify body_class() output to show category-{slug} for single posts (12 posts)

  1. mpmchugh
    Member
    Posted 4 years ago #

    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

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    <?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 ); ?>>
  3. mpmchugh
    Member
    Posted 4 years ago #

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

  4. fisherwebdev
    Member
    Posted 4 years ago #

    thanks esmi!

  5. Circuitbomb
    Member
    Posted 4 years ago #

    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

  6. groomedmonkey
    Member
    Posted 4 years ago #

    <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.

  7. groomedmonkey
    Member
    Posted 4 years ago #

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

  8. eastwoodarts
    Member
    Posted 4 years ago #

    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;
    }
  9. KerriM
    Member
    Posted 4 years ago #

    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.

  10. KerriM
    Member
    Posted 4 years ago #

    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…)

  11. ambrosite
    Member
    Posted 4 years ago #

    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.

    http://wordpress.org/extend/plugins/ambrosite-body-class-enhanced/

  12. kmaier
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic