2015-04-15 38 views
0

我試圖做出不同的身體類,當我訪問某些類別時,它的工作完美,除了當我嘗試創建新類別然後訪問類別帖子時,它將回顯特定類別類別而不是正常的body_class();body_class wordpress加載其他類

這裏的代碼: 的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();

簡而言之:我只想在category-1中回顯特定的類,否則使用正常的body_class(); 。

任何想法爲什麼它不工作?

回答

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

相關問題