要获取具有特定类别的自定义帖子类型,请使用自定义分类法注册自定义帖子类型类类别的分类法名称,然后在添加新帖子时为每个帖子分配一个类别。下面是代码的示例
add_action( \'init\', \'news_my_taxonomy\');
function news_my_taxonomy(){
// custom post type taxonomies
$labels = array(
\'name\' => \'Categories\',
\'singular_name\' => \'Category\',
\'add_new\' => \'Add Category\',
\'add_new_item\' => \'Add New Category\',
\'all_items\' => \'All Categories\',
\'edit_item\' => \'Edit Item\',
\'new_item\' => \'New Item\',
\'view_item\' => \'View Item\',
\'update_item\' => \'Update Category\',
\'search_items\' => \'Search Categories\',
\'not_found\' => \'No record found\',
\'not_found_in_trash\' => \'No items found in trash\',
\'parent_item_colon\' => \'Parent Item\',
\'menu_name\' => \'Categories\'
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'news_category\'),
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
);
register_taxonomy(\'news_category\', array(\'news\'), $args);
}
然后创建分类模板页面“taxonomy-news\\u类别”。php“并添加查询以获取具有此类别名称的帖子
$cat_name = single_cat_title;
$args = array( \'category_name\' => $cat_name, \'posts_per_page\' => 12, \'order\'=> \'ASC\', \'post_type\' => \'news\', \'paged\' => $paged);
所有的工作都完成了。祝你好运