How to add Category name to body class wordpress!

how to show category name to body class wordpress

What is the purpose of body_class function in wordpress ?

body_class function helps to show the bunch of classes name to the body element that have information about what kind of page is currently being displayed.

Here is the way to implement body_class function into theme template.

<body <?php body_class(); ?>>

So now we want to add category name to body class .. mostly for styling purposes. 😉

Add this code to your functions.php file and hit on save button.

add_filter('body_class','nst_add_category_name');
function nst_add_category_name($classes) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug name to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array here
return $classes;
}

I hope this helps you.. Thanks 🙂

If this help you then.. Can you buy a Cup of Coffee for me by nst webcreation paypal--OR-- nst webcreation blog coffee cup

Published by

Narender Singh

Myself Narender Singh Thakur ( NST ) and i share my Experience/Knowledge and Tricks for folks and beginners to solve their issues while making websites through this Planet.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Click to Chat