如何在WordPress中将类别添加到自定义帖子类型
最近,我们的一位用户问我们是否可以将类别添加到他们创建的自定义帖子类型中。类别是WordPress中的内置分类法之一。默认情况下,它们仅适用于帖子。但是,在某些情况下,您可能还希望与自定义帖子类型共享它们。在本文中,我们将向您展示如何在WordPress中向自定义帖子类型添加类别。我们还将向您展示如何在类别存档页面上显示多个帖子类型。

插件方法
对于我们的初级用户,我们建议使用自定义帖子类型UI插件来创建自定义帖子类型。使用自定义帖子类型UI插件时,您可以选择将自定义帖子类型与任何内置或自定义分类(包括类别)相关联。
首先,您需要安装并激活自定义帖子类型UI插件。有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。
安装后,您需要访问 CPT UI»添加/编辑帖子类型创建新的自定义帖子类型或编辑您使用该插件创建的现有自定义帖子类型。

向下滚动高级选项到底部,您将看到内置Taxnomies 选项。选中类别旁边的框并保存自定义帖子类型。

不要忘记单击保存帖子类型按钮来存储您的设置。
手动将类别添加到自定义帖子类型
如果您通过在主题的functions.php文件或特定于站点的插件中添加代码来创建自定义帖子类型,那么您将不得不修改代码以将类别添加为支持的分类。
您需要做的就是在CPT的参数中添加此行。
"taxonomies" => array( "category" ),
您可能已经在CPT的现有代码中使用了此行,其中包含其他一些自定义分类。如果你这样做,那么你只需要在此之后添加一个逗号并添加类别,如下所示:
"taxonomies" => array("topics", "category" ),
这是一个完整的示例代码,我们创建了一个名为movies的自定义帖子类型,支持内置类别。
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
"name" => _x( "Movies", "Post Type General Name", "twentythirteen" ),
"singular_name" => _x( "Movie", "Post Type Singular Name", "twentythirteen" ),
"menu_name" => __( "Movies", "twentythirteen" ),
"parent_item_colon" => __( "Parent Movie", "twentythirteen" ),
"all_items" => __( "All Movies", "twentythirteen" ),
"view_item" => __( "View Movie", "twentythirteen" ),
"add_new_item" => __( "Add New Movie", "twentythirteen" ),
"add_new" => __( "Add New", "twentythirteen" ),
"edit_item" => __( "Edit Movie", "twentythirteen" ),
"update_item" => __( "Update Movie", "twentythirteen" ),
"search_items" => __( "Search Movie", "twentythirteen" ),
"not_found" => __( "Not Found", "twentythirteen" ),
"not_found_in_trash" => __( "Not found in Trash", "twentythirteen" ),
);
// Set other options for Custom Post Type
$args = array(
"label" => __( "movies", "twentythirteen" ),
"description" => __( "Movie news and reviews", "twentythirteen" ),
"labels" => $labels,
"supports" => array( "title", "editor", "excerpt", "author", "thumbnail", "comments", "revisions", "custom-fields", ),
"hierarchical" => false,
"public" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"show_in_admin_bar" => true,
"menu_position" => 5,
"can_export" => true,
"has_archive" => true,
"exclude_from_search" => false,
"publicly_queryable" => true,
"capability_type" => "page",
// This is where we add taxonomies to our CPT
"taxonomies" => array( "category" ),
);
// Registering your Custom Post Type
register_post_type( "movies", $args );
}
/* Hook into the "init" action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( "init", "custom_post_type", 0 );
在类别页面上显示多个帖子类型
默认情况下,WordPress网站上的类别页面将仅显示默认的“帖子”帖子类型。要在与默认帖子相同的类别页面上显示自定义帖子类型,您需要将此代码添加到主题的functions.php或特定于站点的插件中。
add_filter("pre_get_posts", "query_post_type");
function query_post_type($query) {
if( is_category() ) {
$post_type = get_query_var("post_type");
if($post_type)
$post_type = $post_type;
else
$post_type = array("nav_menu_item", "post", "movies"); // don"t forget nav_menu_item to allow menus to work!
$query->set("post_type",$post_type);
return $query;
}
}
不要忘记用您自己的自定义帖子类型的名称替换电影。
这就是全部,我们希望本文能帮助您在WordPress中为自定义帖子类型添加类别。您也可以使用相同的方法为自定义帖子类型添加标签。

评论被关闭。