Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

body_class wordpress doesn’t work

Tags:

php

wordpress

I was trying to place a class inside the body tag of the html with php for my wordpress theme. I look at the codex of wordpress and I should use the following, and add it to my functions.php file:

add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
    // add 'class-name' to the $classes array
    $classes[] = 'class-name';
    // return the $classes array
    return $classes;
}

Only does this not work for me. Does anyone have any idea what this could be?

like image 613
Caspert Avatar asked Sep 21 '26 10:09

Caspert


1 Answers

This code appears to be correct.

So you have to make sure that you're calling body_class() correctly in the theme in the opening body tag. It is usually found in header.php and should be similar to this:

<body <?php body_class(); ?>>
like image 93
Marin Atanasov Avatar answered Sep 24 '26 06:09

Marin Atanasov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!