最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create category only for custom post type

programmeradmin1浏览0评论

All, I'm creating my own post type using the following code:

add_action( 'init', 'create_team_post_type' );
function create_team_post_type() {
  register_post_type( 'team',
    array(
      'labels' => array(
        'name' => __( 'Teams' ),
        'singular_name' => __( 'Team' )
      ),
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('category'),
        'supports' => array('title','editor','thumbnail')
    )
  );
}

In this I'm allowing a category. When I click on the category for this I see the categories already listed from my post categories. I'd like to only show the categories for this custom post type. In addition when I add a new category to the custom post type I only want it to apply to this custom post type. How do I go about doing this?

All, I'm creating my own post type using the following code:

add_action( 'init', 'create_team_post_type' );
function create_team_post_type() {
  register_post_type( 'team',
    array(
      'labels' => array(
        'name' => __( 'Teams' ),
        'singular_name' => __( 'Team' )
      ),
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('category'),
        'supports' => array('title','editor','thumbnail')
    )
  );
}

In this I'm allowing a category. When I click on the category for this I see the categories already listed from my post categories. I'd like to only show the categories for this custom post type. In addition when I add a new category to the custom post type I only want it to apply to this custom post type. How do I go about doing this?

Share Improve this question asked Nov 16, 2014 at 19:51 user1048676user1048676 4373 gold badges9 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

I would say that you need to also create a custom taxonomy if you want it to be limited to the one post type. "Categories" is already connected to posts by default.

From the WordPress Codex

function people_init() {
    // create a new taxonomy
    register_taxonomy(
        'people',
        'post',
        array(
            'label' => __( 'People' ),
            'rewrite' => array( 'slug' => 'person' ),
            'capabilities' => array(
                'assign_terms' => 'edit_guides',
                'edit_terms' => 'publish_guides'
            )
        )
    );
}
add_action( 'init', 'people_init' );

So, if you called it "team-category", you would then use that in the 'taxonomies' array in your post type.

Here's a more specific example:

function tr_create_my_taxonomy() {

    register_taxonomy(
        'team-category',
        'team',
        array(
            'label' => __( 'Category' ),
            'rewrite' => array( 'slug' => 'team-category' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );
发布评论

评论列表(0)

  1. 暂无评论