开发者

Re-use default categories/tags taxonomies for custom post type?

In my wordpress theme's functions.php, I've created a custom post type called foobar.

Is it possible to re-use the default categories 开发者_如何学编程and tags for foobar posts? Or do I have to create two taxonomies:

  • one for categories
  • the other for tags

to achieve this?

EDIT: I think I've solved this by using this code within the function that creates the custom post type:

register_taxonomy_for_object_type('category', 'foobar');
register_taxonomy_for_object_type('post_tag', 'foobar');


You can kill two birds with one stone and use the taxonomies key when registering the post type:

$product_labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Product', 'post type singular name'),
        'add_new' => _x('Add New', 'portfolio item'),
        'add_new_item' => __('Add New Product'),
        'edit_item' => __('Edit Product'),
        'new_item' => __('New Product'),
        'view_item' => __('View Product'),
        'search_items' => __('Search Products'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );
    $product_args = array(
        'labels' => $product_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'product'),
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 2,
        'supports' => array('title','editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'page-attributes', 'custom-fields', ),
        // Set the available taxonomies here
        'taxonomies' => array('category', 'post_tag') 
    );
    register_post_type('product', $product_args);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜