• Hey Folks,

    I am trying to add a blog category class dynamically to the body class. IE. I want the body tag to have a class added depending on the Blog SECTION you are in.

    For example – I have a blog category called writing tips and would like that classed added. Currently this is what it shows.

    <body class=”single single-post postid-91 logged-in mac firefox ff3″>

    Any help is appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi dreeft,
    try <?php body_class( $class ); ?>
    look at http://codex.wordpress.org/Function_Reference/body_class fot more details

    following the idea of @davidandre:

    <?php if( is_single() ) { $cats = get_the_category( $post->ID );
    $body_class_cat_single = array();
    foreach( $cats as $cat ) {
    $body_class_cat_single[] = 'cat-' . $cat->slug;
    }
     }
     ?>
    
    <body <?php body_class($body_class_cat_single); ?>>

    'cat-' is the fixed part of the css class – use what you like, however, don’t use 'category-' because this is used in category archives and by post_class()

    if you prefer category ids – use this:
    $body_class_cat_single[] = 'cat-' . $cat->term_id;

    Thread Starter dreeftwood

    (@dreeftwood)

    Thanks folks,
    That is what I was looking for. I saw the body-class but was not sure how to call the category.

    I think what alchymyth suggestion might be the one.

    THanks again, will give it a try.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Dynamic classes in body tag?’ is closed to new replies.