使用自定义分类法在Wordpress中创建自定义帖子类型 [英] Creating a Custom Post-Type in Wordpress with Custom Taxonomy

查看:103
本文介绍了使用自定义分类法在Wordpress中创建自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我要销售的玩具建立自定义帖子类型。我要创建的自定义帖子类型是玩具。我希望它们具有类别/标签,以便以后进行排序。我现在要创建的标签是沐浴玩具,磁铁, Yoyos和黑暗中发光。

I need to build a custom post type for the Toys I'm selling. The custom post type I want to create is "Toys". I want them to have a categories/tags so I can sort them later, Tags I want to create for now is "Bath Toys", "Magnets", "Yoyos", and "Glow in the Dark".

我认为,如果我能观察到代码,则可以尝试对其进行分析并在以后进行复制。

I think if I can observe the code, I can try to analyze it and just replicate it later.

这是我去过的教程试图跟随。但这仍然让我困惑如何添加分类法或标签。

Here's the tutorial that I've been trying to follow. But it still confuses me how to add taxonomies or tags.

我正在将此函数添加到 functions.php 我的孩子主题,我正在使用WordPress 3.3.1

I am adding this functions to the functions.php of my child theme and I am using WordPress 3.3.1

推荐答案

您要在函数中定义分类法和自定义帖子类型.php使用 register_taxonomy() register_post_type()函数。

You want to define your taxonomy and custom post type in your functions.php using the register_taxonomy() and register_post_type() functions.

以下是一个示例:

/****************************************
 * 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 );
}

然后您需要转到管理员后端,您应该看到玩具在发布下方,在玩具类别中创建所需的类别。

Then you need to go to the admin back-end and you should see Toys right below Post, create categories you desire in 'Toys Categories'.

这篇关于使用自定义分类法在Wordpress中创建自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆