Raviv g
Raviv g

Reputation: 29

body_class wordpress load other class

i trying to make different body classes when i am visit some category , its working perfect except when i try to create new category and then visit the category post , it will echo the specific category class instead of the normal body_class();

here the code: header.php:

<?php

$catid1 = get_cat_ID( 'פאודה צפייה ישירה' );

$currentcatID = the_category_ID($echo=false);

if(is_single() && $catid1 == $currentcatID){
 $catid1 = $catid1;
}else{
 $catid1 = $currentcatID;
}
?>

<body <?php

    if(!empty($currentcatID)){
        if(is_single()){
            if($catid1 == $currentcatID){
            echo 'class = "rtl single single-post category-'.$catid1.' postid-81 single-format-standard featured-image-only"';
            }else{
              body_class();
            }
        }else{
              body_class();
        }
    }
   ?>>

When i visit index site all working perfect , when i visit page with that category id also working perfect but when create new category and add test post , its echo the first class echo 'class = "rtl single single-post category-'.$catid1.' postid-81 single-format-standard featured-image-only"'; which i don't want , i want to echo body_class();

Short : i want to echo the specific class only at category-1 else use normal body_class(); .

Any idea why it's not working ?

Upvotes: 0

Views: 219

Answers (1)

Mathew Tinsley
Mathew Tinsley

Reputation: 6976

You should use the body_class filter rather than trying to recreate the body class in certain situations.

If I'm understanding correctly, you want to add a certain class to the body for a particular category. This should accomplish that without reinventing body_class

add_filter('body_class', function($classes) {
    $catid = get_cat_ID( 'פאודה צפייה ישירה' );
    $currentcatID = get_query_var('cat');
    if($catid == $currentcatID) {
        $classes[] = 'my-additional-class';
    }
    return $classes;
});

Make sure that filter is added before you call body_class, usually you would just add it to your theme's functions.php file.

https://codex.wordpress.org/Plugin_API/Filter_Reference/body_class

Upvotes: 1

Related Questions