首页 文章

body_class wordpress加载其他类

提问于
浏览
0

当我访问某个类别时,我试图制作不同的身体类,它的工作完美,除非我尝试创建新类别然后访问类别帖子,它将回显特定类别类而不是正常的body_class();

这里的代码: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();
        }
    }
   ?>>

当我访问索引网站所有工作完美,当我访问该类别ID的页面也工作完美但是当创建新类别并添加测试帖子时,它回显第一类 echo 'class = "rtl single single-post category-'.$catid1.' postid-81 single-format-standard featured-image-only"'; 我不想要,我想回显body_class() ;

简短:我想仅在类别1回应特定类,否则使用普通的body_class(); .

知道为什么它不起作用吗?

1 回答

  • 1

    您应该使用 body_class 过滤器,而不是尝试在某些情况下重新创建主体类 .

    如果我理解正确,您希望为特定类别添加某个类到正文 . 这应该完成,而不是重新发明 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;
    });
    

    确保在调用 body_class 之前添加了过滤器,通常只需将其添加到主题的 functions.php 文件中 .

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

相关问题