将类别和标签添加到页面并作为类插入到正文中

你可以将此代码添加到自定义 functions.php 文件中:

// add tags and categories to pages

function add_taxonomies_to_pages() {
    register_taxonomy_for_object_type( 'post_tag', 'page' );
    register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_taxonomies_to_pages' );

if ( ! is_admin() ) {
    add_action( 'pre_get_posts', 'category_and_tag_archives' );
}
 
function category_and_tag_archives( $wp_query ) {
$my_post_array = array('post','page');

    if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
    $wp_query->set( 'post_type', $my_post_array );
    
    if ( $wp_query->get( 'tag' ) )
    $wp_query->set( 'post_type', $my_post_array );
    
}

// add tags and categorys as class to <body>

function add_categories_and_tags( $classes = '' ) {
    if( is_page() ) {
        $categories = get_the_category();
        foreach( $categories as $category ) {
            $classes[] = 'category-'.$category->slug;
        }
        $tags = get_the_tags();
        foreach( $tags as $tag ) {
            $classes[] = 'tag-'.$tag->slug;
        }
    }
return $classes;
}
add_filter( 'body_class', 'add_categories_and_tags' );

完美,在 WordPress 4.8 中测试过 ****