跳到主要内容

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

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

插件方法

对于初学者用户,我们建议使用“自定义帖子类型” UI插件来创建自定义帖子类型。使用“自定义帖子类型” UI插件时,您可以选择将自定义帖子类型与任何内置或自定义分类法(包括类别)相关联。

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

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

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

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

手动将类别添加到自定义帖子类型

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

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

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

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

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

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

functioncustom_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');functionquery_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;    }}

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

回到顶部