0
votes

Recently I have been working on creating my first wordpress theme. However, when I was trying to add classes to body tag using PHP, the Chrome inspector shows that no classes have been added at all.

  1. I have tried to move the body_class() PHP line out of the tag, and the appropriate class="(some classes)" loaded up successfully, but when the tag include the PHP scripts, no classes were added.
  2. Manually adding classes inside the body tag in header.php does not work as well.

Is there any solution? Because my header.php should be following the right logic.

A link to my files:

https://drive.google.com/open?id=1h9YvmYEQYiHWI-N0lkelAUbVbEQwTelz

1
Is there any chance of getting the whole theme as you have it now as a zip file? if so, please share via drive. - Cesar Correchel
I have checked your code, It's working fine here. Is there any console error? - Ali
I have added the zip file on the drive @CesarCorrechel. - Mr.Benson
When I use F12 inspector to check the classes of body on my local WordPress, the classes still did not exist @Sarbaz - Mr.Benson
May be you are changing in another theme, Please remove your whole code from header.php, then check that's changes effected in the site or not. If the theme is correct maybe the issue for caching. your code is perfect... :) - Ali

1 Answers

0
votes

try that

/** 
* Adds custom classes to the array of body classes. 
*
* @param array $classes Classes for the body element. 
* @return array 
*/ 
 function White_theme_body_classes( $classes ) {
    // Adds a class of group-blog to blogs with more than 1 published author.
    if ( is_multi_author() ) {
        $classes[] = 'group-blog';
    }

    // Adds a class of hfeed to non-singular pages.
    if ( ! is_singular() ) {
        $classes[] = 'hfeed';
    }

    return $classes;
 } 
add_filter( 'body_class', 'White_theme_body_classes' );

and in header.php add this code

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