Body Classes Extended

I recently worked in a website that had multiple skins within a single domain. The different themes were controlled and activated by their related parent pages. I’ve already gone over how to add an ancestor class to the body, but I needed to add a couple additional features to this website as well.

Add Category Class to Body:

function add_category_to_single($classes, $class) {
  if (is_single() || is_category() ) {
    global $post;
    foreach((get_the_category($post->ID)) as $category) {
      // add category slug to the $classes array
      $classes[] = $category->category_nicename;
  // return the $classes array
  return $classes;

Reference here.

Check if Body Class Exists to see if on Category Parent:

$class_check = get_body_class();
    if( in_array('category-slug', $class_check) { 
        // content here

Reference here.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.