2017-08-10 25 views

回答

0

您需要使用下面的代码在页脚中的具体职位追加CSS挂接到wp_footer的类别。

12是id特定的帖子。你需要改变你需要调用CSS的帖子的ID。 is_category('categoryname')按类别检查类别slug/name。

add_action('wp_footer',function(){ 
global $post; 
if(is_category('categoryname') && $post->ID == 12) 
echo '<style type="text/css">.change_color:{color:red}</style>'; 
}) 

如果你有自定义分类使用..

变化参数按需要。这个函数将检查你的当前帖子是否有特定的类别,它会改变你的选择器的颜色。

add_action('wp_footer',function(){ 
    global $post; 
    $cat_id = get_query_var('cat'); 
    if(has_term($cat_id , 'my_custom_post_type')) 
    echo '<style type="text/css">.change_color:{color:red}</style>'; 
    }) 
+0

谢谢诺曼,但上面的编辑将要求我编辑(并添加)每一个新职位的新的代码集,我呢?我想要的是一个特定类别的所有帖子的CSS,希望它是有道理的。 –

+0

@IrfanSaleem代码的第二部分将适用于所有帖子,如果有特定的类别。 'has_term'将检查每个帖子是否具有特定类别。这意味着每个帖子都有一个css规则 – Noman

0

我已经完成了。我添加了一个自定义函数。我在这里找到 https://philipnewcomer.net/2012/10/add-post-categories-to-the-body-class-in-wordpress/

function pn_body_class_add_categories($classes) { 

    // Only proceed if we're on a single post page 
    if (!is_single()) 
     return $classes; 

    // Get the categories that are assigned to this post 
    $post_categories = get_the_category(); 

    // Loop over each category in the $categories array 
    foreach($post_categories as $current_category) { 

     // Add the current category's slug to the $body_classes array 
     $classes[] = 'category-' . $current_category->slug; 

    } 

    // Finally, return the $body_classes array 
    return $classes; 
} 
add_filter('body_class', 'pn_body_class_add_categories'); 

之后,这只是基本的,得到了​​身体的范畴类,有针对性的单篇文章类。我希望它能帮助别人。

相关问题