
WordPress 定制
Recently one of our user asked us if it was possible to add 类别 to a 自定义帖子类型 they have created. Categories are one of the built-in 分类法 in WordPress. By default they appear only for posts. However, in some scenarios you may want to share them with a custom post type as well. In this article, we will show you how to add categories to a custom post type in WordPress. We will also show you how to display multiple post types on your category archive page.
最近,我们的一位用户问我们是否可以将类别添加到他们创建的自定义帖子类型 。 类别是WordPress中的内置分类之一。 默认情况下,它们仅显示在帖子中。 但是,在某些情况下,您可能还需要与自定义帖子类型共享它们。 在本文中,我们将向您展示如何在WordPress中为自定义帖子类型添加类别。 我们还将向您展示如何在类别存档页面上显示多种帖子类型。

插件方法 (The Plugin Method)
For our beginner level users, we recommend using Custom Post Type UI plugin to 创建自定义帖子类型. When using Custom Post Type UI plugin, you have the option to associate your custom post type to any built-in or custom taxonomy including categories.
对于初学者用户,我们建议使用“自定义帖子类型” UI插件来创建自定义帖子类型 。 使用“自定义帖子类型” UI插件时,您可以选择将自定义帖子类型与任何内置或自定义分类法(包括类别)相关联。
First you need to install and activate the 自定义帖子类型 UI plugin. For more details, see our step by step guide on 如何安装 WordPress 插件.
首先,您需要安装并激活“ 自定义帖子类型”UI插件。 有关更多详细信息,请参阅有关如何安装 WordPress 插件步指南。
Upon installation, you need to visit CPT UI » 添加/编辑帖子类型 to create a new custom post type or edit an existing custom post type you created with the plugin.
安装后,您需要访问CPT UI » 添加/编辑帖子类型以创建新的自定义帖子类型或编辑使用插件创建的现有自定义帖子类型。

Scroll down on the Advanced Options to the bottom and there you will see the 内置分类法 option. Check the box next to categories and save your custom post type.
向下滚动到底部的“高级选项”,您将在里面看到“ 内置分类法”选项。 选中类别旁边的框,然后保存您的自定义帖子类型。

不要忘记单击“保存帖子类型”按钮来存储您的设置。
不要忘记单击“保存帖子类型”按钮来存储您的设置。
手动将类别添加到自定义帖子类型 (Manually Adding Categories to a Custom Post Type)
If you created your custom post type by adding the code in your theme’s 函数.php file or a 特定于站点的插件, then you will have to modify the code to add category as supported taxonomy.
如果您通过在主题的函数.php文件或具体的于在网站的插件中添加代码来创建自定义帖子类型,则必须修改代码以将类别添加为受支持的分类法。
您所需要做的就是在 CPT 的参数中添加这一行。
您所需要做的就是将此行添加到 CPT 的参数中。
'taxonomies' => array( 'category' ),
您的 CPT 的现有代码中可能已经有这一行,其中还包含一些其他自定义分类法。如果这样做,那么您只需要在后面添加一个逗号并添加类别,如下所示:
此行可能已经包含在 CPT 的现有代码中,以及一些其他自定义分类法。如果这样做,您只需在其后添加一个逗号并添加类别,如下所示:
'taxonomies' => array('topics', 'category' ),
这是一个完整的示例代码,我们在其中创建了一个名为“电影”的自定义帖子类型,并支持内置类别。
这是完整的示例代码,我们在其中创建了一个名为“电影”的自定义帖子类型,并支持内置类别。
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 );
在类别页面上显示多种帖子类型 (Displaying Multiple Post Types on Category Page)
By default the 类别页面 on your WordPress site will only display the default ‘Posts’ post type. To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php or a site-specific plugin.
默认情况下,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 中向自定义帖子类型添加类别。您也可以使用相同的方法将标签添加到自定义帖子类型。
就这样,我们希望本文能帮助您在 WordPress 中向自定义帖子类型添加类别。您还可以使用相同的方法将标签添加到自定义帖子类型。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and 谷歌+.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和谷歌+上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-categories-to-a-custom-post-type-in-wordpress/
WordPress 定制