重新使用自定义帖子类型的默认类别/标签分类法?

发布于 2024-11-13 11:14:55 字数 400 浏览 5 评论 0原文

在我的 WordPress 主题的 functions.php 中,我创建了一个名为 foobar 的自定义帖子类型。

是否可以重新使用 foobar 帖子的默认类别和标签?或者我是否必须创建两种分类法:

  • 一种用于类别,
  • 另一种用于标签

才能实现此目的?

编辑:我想我已经通过在创建自定义帖子类型的函数中使用此代码解决了这个问题:

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

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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-11-20 11:14:55

您可以一石二鸟,并在注册帖子类型taxonomies键>:

$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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文