How to Add the Parent Category as a Class in body_class()


<?php $class=''; /* Make it void by default */

if ( is_single() || is_category() ) { /* Only do this if we're on a single post or category page */
$category = get_the_category();
$parent = $category[0]->category_parent;
$class = get_cat_name($parent); /* assign the permalink name for the parent category to the object $class */
}?>

<body <?php body_class($class); /* This will add our custom class, which is void if we aren't on a single post or category page */ ?>>

Comments

Popular posts from this blog

HTML5 video loop src change on end play function

Set limit wp_nav_menu

Calculate percentage of a specific element has been scrolled into visible on the screen