在自定义帖子类型中显示标签面板


Show Tag panel in custom post type

我只是做了一个自定义的帖子类型。如何在侧边栏上显示标签面板,与帖子类型相同?

将此

行添加到您在函数中register_post_type的部分.php在主题文件夹中

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

完整的代码看起来像这样

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'posttypename',
        array(
            'labels' => array(
                'name' => __( 'PostTypeName' ),
                'singular_name' => __( 'PostTypeName' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'posttypename'),
            'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ),
            'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
        )
    );
}
如果您

使用'taxonomies' => array('category', 'post_tag')则wordpress默认帖子的标签将显示在自定义帖子类型区域中。

这是"新闻"帖子类型的干净而独特的方式。不与其他自定义帖子类型,默认标签混合,..等。

您可以从此链接中关注"创建自定义帖子类型和带有类别的标签"的完整详细信息。

add_action( 'init', 'news_tag_taxonomies' ); //change order add_action( 'init', 'news_tag_taxonomies', 0 );
//create two taxonomies, genres and tags for the post type "tag"
function news_tag_taxonomies() 
{
  // Add new taxonomy, NOT hierarchical (like tags)
  $labels = array(
    'name' => _x( 'Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Tags' ),
    'popular_items' => __( 'Popular Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Tag' ), 
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'separate_items_with_commas' => __( 'Separate tags with commas' ),
    'add_or_remove_items' => __( 'Add or remove tags' ),
    'choose_from_most_used' => __( 'Choose from the most used tags' ),
    'menu_name' => __( 'Tags' ),
  ); 
  register_taxonomy('tag','news',array( // replace your post type with "news"
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tag' ),
  ));
}

希望这会有所帮助。

除了默认的 WP codex 模板之外,我需要添加的只是

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