不过滤自定义帖子类型的类别

时间:2014-03-14 11:19:06

标签: php wordpress

我无法使用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 );

CUSTOM POST TYPE QUERY

<? $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(); ?>

2 个答案:

答案 0 :(得分:1)

您在上面的查询中没有通过自定义分类进行任何类型的过滤。不仅仅是为什么你要运行这个查询。

创建一个名为“taxonomy-tagmusic.php”的模板文件。

然后只使用常规循环。

E.g。

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

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

答案 1 :(得分:-1)

add_action( 'init', 'register_cpt_music' );

function 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 );

}