Forums

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

  1. mpmchugh
    Member
    Posted 3 months 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
    Member
    Posted 3 months 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 3 months ago #

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

  4. fisherwebdev
    Member
    Posted 1 month ago #

    thanks esmi!

  5. Circuitbomb
    Member
    Posted 1 month 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

Reply

You must log in to post.

About this Topic