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

时间:2012-04-20 11:10:24

标签: php wordpress custom-post-type

我刚做了一个自定义帖子类型。如何在侧边栏上显示与发布帖子类型相同的标记面板?

3 个答案:

答案 0 :(得分:11)

将此行添加到主题文件夹

中functions.php中的register_post_type部分
'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
        )
    );
}

答案 1 :(得分:5)

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

这是" news"的清洁和独特方式。发布类型。不与其他自定义帖子类型,默认标签,等等混合

您可以关注"create custom post types and tags with categories" from this link.

的完整详情
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' ),
  ));
}

希望这会有所帮助。

答案 2 :(得分:1)

除了默认的WP codex模板之外我需要添加的所有内容

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