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

Custom OpenCart 2.3.0.2 theme

PHP: Date Difference for PHP 5.2