首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用自定义分类在Wordpress中创建自定义Post-Type

使用自定义分类在Wordpress中创建自定义Post-Type
EN

Stack Overflow用户
提问于 2012-02-26 03:37:28
回答 2查看 3.9K关注 0票数 2

我需要为我销售的玩具建立一个自定义的帖子类型。我想创建的自定义帖子类型是“玩具”。我希望它们有一个类别/标签,这样我以后就可以对它们进行排序,我现在想创建的标签是“洗浴玩具”,“磁铁”,"Yoyos“和”黑暗中的发光“。

我想如果我能观察到代码,我就可以尝试分析它,然后再复制它。

这是我一直在尝试关注的the tutorial。但它仍然让我困惑如何添加分类法或标签。

我正在将这个函数添加到我的子主题的functions.php中,并且我使用的是WordPress 3.3.1

EN

回答 2

Stack Overflow用户

发布于 2012-10-02 08:21:24

您希望使用register_taxonomy()register_post_type()函数在functions.php中定义分类和自定义post类型。

下面是一个示例,它可能是这样的:

代码语言:javascript
复制
/****************************************
 * Add custom taxonomy for Toys *
 ****************************************/

add_action('init', 'toys_categories_register');

function toys_categories_register() {
$labels = array(
    'name'                          => 'Toys Categories',
    'singular_name'                 => 'Toys Category',
    'search_items'                  => 'Search Toys Categories',
    'popular_items'                 => 'Popular Toys Categories',
    'all_items'                     => 'All Toys Categories',
    'parent_item'                   => 'Parent Toy Category',
    'edit_item'                     => 'Edit Toy Category',
    'update_item'                   => 'Update Toy Category',
    'add_new_item'                  => 'Add New Toy Category',
    'new_item_name'                 => 'New Toy Category',
    'separate_items_with_commas'    => 'Separate toys categories with commas',
    'add_or_remove_items'           => 'Add or remove toys categories',
    'choose_from_most_used'         => 'Choose from most used toys categories'
    );

$args = array(
    'label'                         => 'Toys Categories',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'toys', 'with_front' => true, 'hierarchical' => true ),
    'query_var'                     => true
);

register_taxonomy( 'toys_categories', 'toys', $args );
}

/*****************************************
 * Add custom post type for Toys *
 *****************************************/

add_action('init', 'toys_register');

function toys_register() {

    $labels = array(
        'name' => 'Toys',
        'singular_name' => 'Toy',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Toy',
        'edit_item' => 'Edit Toy',
        'new_item' => 'New Toy',
        'view_item' => 'View Toy',
        'search_items' => 'Search Toys',
        'not_found' =>  'Nothing found',
        'not_found_in_trash' => 'Nothing found in Trash',
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'toys', 'with_front' => true ),
        'capability_type' => 'post',
        'menu_position' => 6,
        'supports' => array('title', 'excerpt', 'editor','thumbnail') //here you can specify what type of inputs will be accessible in the admin area
      );

    register_post_type( 'toys' , $args );
}

然后你需要去管理员后端,你应该看到玩具下面的帖子,创建你想要的类别在‘玩具类别’。

票数 4
EN

Stack Overflow用户

发布于 2015-06-06 04:18:41

我知道我来晚了,但这可能会帮助那些正在努力找到一种简单的方法来添加可湿性粉剂自定义帖子类型。

有一个很棒的库来处理WordPress帖子类型和分类法。

采取这些步骤,这将使你的生活变得更简单。

主题目录中的

  1. 运行此命令。

主题functions.php的$ composer require azi/raskoh

  • include composer自动加载器

require_once "vendor/autoloader.php";

  • after要求添加此代码以注册post类型

`

代码语言:javascript
复制
$toy = new Raskoh\PostType("Toy");

$toy->register();

Raskoh (库)将会处理剩下的事情。这是图书馆

Github

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9447366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档