wordpress 自定义_如何在WordPress中为自定义帖子类型添加类别

wordpress 自定义

Recently one of our user asked us if it was possible to add categories to a custom post type they have created. Categories are one of the built-in taxonomies 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中为自定义帖子类型添加类别。 我们还将向您展示如何在类别存档页面上显示多种帖子类型。

Adding categories to a custom post type
插件方法 (The Plugin Method)

For our beginner level users, we recommend using Custom Post Type UI plugin to create custom post types. 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 Custom Post Type UI plugin. For more details, see our step by step guide on how to install a WordPress plugin.

首先,您需要安装并激活“ 自定义帖子类型” UI插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon installation, you need to visit CPT UI » Add/Edit Post Types to create a new custom post type or edit an existing custom post type you created with the plugin.

安装后,您需要访问CPT UI»添加/编辑帖子类型以创建新的自定义帖子类型或编辑使用插件创建的现有自定义帖子类型。

Editing post types with CPT UI plugin

Scroll down on the Advanced Options to the bottom and there you will see the Built in Taxnomies option. Check the box next to categories and save your custom post type.

向下滚动到底部的“高级选项”,您将在其中看到“ 内置分类标准”选项。 选中类别旁边的框,然后保存您的自定义帖子类型。

Turn on categories for a Custom Post Type in WordPress

Don’t forget to click on the save post type button to store your settings.

不要忘记单击“保存帖子类型”按钮来存储您的设置。

手动将类别添加到自定义帖子类型 (Manually Adding Categories to a Custom Post Type)

If you created your custom post type by adding the code in your theme’s functions.php file or a site-specific plugin, then you will have to modify the code to add category as supported taxonomy.

如果您通过在主题的functions.php文件或特定站点的插件中添加代码来创建自定义帖子类型,则必须修改代码以将类别添加为受支持的分类法。

All you need to do is add this line in the arguments for your CPT.

您需要做的就是将这行添加到CPT的参数中。


'taxonomies'  => array( 'category' ),

It is likely that you may already have this line in the existing code for your CPT with some other custom taxonomy in it. If you do, then you just need to add a comma after that and add category, like this:

CPT的现有代码中可能已经包含此行,并且其中包含一些其他自定义分类法。 如果这样做,则只需要在其后添加一个逗号并添加类别,如下所示:


'taxonomies'		  => array('topics', 'category' ),

Here is a full example code where we have created a custom post type called movies with support for built-in categories.

这是完整的示例代码,我们在其中创建了一个自定义帖子类型,称为电影,并支持内置类别。


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 category pages 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;
    }
}

Don’t forget to replace movies with the name of your own custom post type.

不要忘记用您自己的自定义帖子类型的名称替换电影。

That’s all, we hope this article helped you add categories to your custom post type in WordPress. You can use the same methods to add tags to your custom post types as well.

仅此而已,我们希望本文能帮助您在WordPress中为自定义帖子类型添加类别。 您也可以使用相同的方法将标签添加到自定义帖子类型。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterGoogle+上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-categories-to-a-custom-post-type-in-wordpress/

wordpress 自定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值