I am trying to add additional classes to <body> based on certain conditions, so I have this code in my functions.php file of the theme :

add_filter( 'body_class', 'custom_body_class' );
function custom_body_class( $classes ) {

if ( !is_home() && !is_archive() ) {
    $classes[] = array_push( $classes, 'new-class' );

if ( condition ) {
    $classes[] = array_push( $classes, 'conditional-class' );

return $classes;

The code above works but when I look at the body class in the HTML output, it will show an additional number as the class depending on its location in the array, for example :

<body class="single logged-in new-class 4 conditional-class 6">

Or :

<body class="single logged-in admin-bar new-class 5 conditional-class 7">

I think this is probably more of a problem with my PHP code but I can't see what's causing it. Any ideas how I can add the conditional body classes without this problem? Thanks for helping!

1 Answer 1


You are using array_push() incorrectly.

Do this:

$classes[] = 'new-class';

Or this:

array_push( $classes, 'new-class' );

But not:

$classes[] = array_push( $classes, 'new-class' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.