You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让自定义文章类型支持body添加分类与标签类名的代码生效?

Got it, let's fix your code to work with custom post types! The main issue with your original function is that it only checks for static pages using is_page(), which excludes posts and custom post types. Here's how to update it:

Modified Code

<?php
/**
 * Add categories and tags to body classes for all singular post types (including CPTs)
 *
 * @param array $classes Existing body classes
 * @return array Modified body classes
 */
function add_categories_and_tags($classes = array()) {
    // Target any single post, page, or custom post type
    if (is_singular()) {
        // Add default category classes (works for posts, pages (if enabled), and CPTs with 'category' taxonomy)
        $categories = get_the_category();
        if (!empty($categories)) {
            foreach ($categories as $cat) {
                $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
            }
        }
        
        // Add default tag classes (works for posts and CPTs with 'post_tag' taxonomy)
        $tags = get_the_tags();
        if (!empty($tags)) {
            foreach ($tags as $tag) {
                $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
            }
        }
        
        // Optional: Add custom taxonomy classes for your CPTs (replace with your taxonomy slug)
        // $custom_terms = get_the_terms(get_the_ID(), 'your_custom_taxonomy');
        // if (!empty($custom_terms)) {
        //     foreach ($custom_terms as $term) {
        //         $classes[] = 'tax-' . sanitize_html_class($term->slug, $term->term_id);
        //     }
        // }
    }
    
    return $classes;
}
add_filter('body_class', 'add_categories_and_tags');

Key Changes Explained

  1. Swapped is_page() for is_singular(): This WordPress function returns true for any single entry (post, page, custom post type), so your code now applies to all singular content types.
  2. Added sanitize_html_class(): This ensures the generated class names are valid HTML (no special characters or invalid characters), which is a best practice to avoid frontend issues.
  3. Optional Custom Taxonomy Support: If your CPT uses a custom taxonomy instead of the default categories/tags, uncomment the last section and replace your_custom_taxonomy with your actual taxonomy slug to include those terms in the body classes.

Important Note for CPTs

For the default category/tag classes to appear on your CPT, you need to have registered the CPT with those taxonomies. When you call register_post_type(), include this in your arguments:

'taxonomies' => array('category', 'post_tag'),

If you only want to target specific CPTs (not all), replace is_singular() with:

is_singular(array('post', 'page', 'your_cpt_slug'))

Just swap your_cpt_slug with the slug of your custom post type.

内容的提问来源于stack exchange,提问作者adifatz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:38:28