I want to add an extra class to my <body>
HTML tag in WordPress? Is there a way to do it? Or do I have to hard code it in the theme header?
TheX Fisherman Selected answer as best
I want to add an extra class to my <body>
HTML tag in WordPress? Is there a way to do it? Or do I have to hard code it in the theme header?
Hello.
Yup there is an easy way to do it with a WordPress filter.
Copy and paste the following code in your functions.php of your theme:
function custom_body_class( $classes ) { if ( is_single() ) { $classes[] = 'my-custom-class'; //add class "my-custom-class" to body if we are on a single page } $classes[] = 'my-custom-class-2'; //add class my-custom-class-2 everywhere return $classes; } add_filter( 'body_class', 'custom_body_class' );
Then the result on front-end will look like this if we were on a single page:
<body class="page page-id-xx page-template my-custom-class my-custom-class-2">
And like this if we are on any other page:
<body class="page page-id-xx page-template my-custom-class-2">
As you can see you can add any number of classes with conditional checks or without.
Hope this helps.
Login bellow or Register