未筛选自定义帖子类型的类别


Categories not filtering on custom post type

我无法在Wordpress中使用自定义帖子类型的类别进行筛选。当我单击类别时,它会显示所有帖子,而不是相应的类别。我使用的是自定义分类法,一直在互联网上搜索,但找不到解决方案。分页是有效的,但分类不起作用。我不知道为什么会这样。

注册自定义帖子类型

// Register Custom Post Type
function custom_post_type_music() {
    $labels = array(
        'name'                => _x( 'Music', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Music', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Music', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Music:', 'text_domain' ),
        'all_items'           => __( 'All Music', 'text_domain' ),
        'view_item'           => __( 'View Music', 'text_domain' ),
        'add_new_item'        => __( 'Add New Music Tracks', 'text_domain' ),
        'add_new'             => __( 'New Music Tracks', 'text_domain' ),
        'edit_item'           => __( 'Edit Music', 'text_domain' ),
        'update_item'         => __( 'Update Music', 'text_domain' ),
        'search_items'        => __( 'Search Music', 'text_domain' ),
        'not_found'           => __( 'No Music found', 'text_domain' ),
        'not_found_in_trash'  => __( 'No Music found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'Music', 'text_domain' ),
        'description'         => __( 'Music information pages', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => 'dashicons-format-audio',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'opd-music', $args );
// Initialize Taxonomy Labels
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name', 'text_domain' ),
        'singular_name' => _x( 'Category', 'taxonomy singular name', 'text_domain' ),
        'search_items' =>  __( 'Search Types', 'text_domain' ),
        'all_items' => __( 'All Categories', 'text_domain' ),
        'parent_item' => __( 'Parent Category', 'text_domain' ),
        'parent_item_colon' => __( 'Parent Category:', 'text_domain' ),
        'edit_item' => __( 'Edit Categories', 'text_domain' ),
        'update_item' => __( 'Update Category', 'text_domain' ),
        'add_new_item' => __( 'Add New Category', 'text_domain' ),
        'new_item_name' => __( 'New Category', 'text_domain' ),
    );
    // Register Custom Taxonomy
    register_taxonomy('tagmusic',array('opd-music'), array(
        'hierarchical' => true, // define whether to use a system like tags or categories
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'cat-music' ),
    ));
}
// Hook into the 'init' action
add_action( 'init', 'custom_post_type_music', 0 );

自定义邮件类型查询

<? $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $args = array(
           'post_type' => 'opd-music',
           'order'     => 'ASC',
           'paged'=>$paged,
            );
        $q = new WP_Query($args);
    $loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>

在上面的查询中,您没有按照自定义分类法进行任何筛选。更重要的是,你为什么要运行这个查询。

创建一个名为"taxonom-tagmusic.php"的模板文件。

然后使用正则循环。

例如

if ( have_posts() ) : while ( have_posts() ) : the_post();

当您在标签音乐分类法中查看术语时,将显示此模板文件,并为您完成查询。

add_action( 'init', 'register_cpt_music' );

函数register_cpt_music(({

$labels = array( 
    'name' => _x( 'Music', 'music' ),
    'singular_name' => _x( 'Music', 'music' ),
    'add_new' => _x( 'Add New', 'music' ),
    'add_new_item' => _x( 'Add New Music', 'music' ),
    'edit_item' => _x( 'Edit Music', 'music' ),
    'new_item' => _x( 'New Music', 'music' ),
    'view_item' => _x( 'View Music', 'music' ),
    'search_items' => _x( 'Search Music', 'music' ),
    'not_found' => _x( 'No music found', 'music' ),
    'not_found_in_trash' => _x( 'No music found in Trash', 'music' ),
    'parent_item_colon' => _x( 'Parent Music:', 'music' ),
    'menu_name' => _x( 'Music', 'music' ),
);
$args = array( 
    'labels' => $labels,
    'hierarchical' => true,
    'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
    'taxonomies' => array( 'Categories' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,

    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
);
register_post_type( 'music', $args );

}