使用自定义分类法在Wordpress中创建自定义后期类型

时间:2012-02-25 19:37:28

标签: wordpress custom-post-type

我需要为我正在销售的玩具构建自定义帖子类型。我想要创建的自定义帖子类型是“Toys”。我希望他们有一个类别/标签,以便我可以在以后对它们进行排序。我想要创建的标签是“Bath Toys”,“Magnets”,“Yoyos”和“Glow in the Dark”。

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

这是我一直想要关注的the tutorial。但它仍然让我感到困惑,如何添加分类法或标签。

我将此功能添加到我的子主题的functions.php中,并且我正在使用WordPress 3.3.1

2 个答案:

答案 0 :(得分:3)

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

以下是一个示例:

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

然后你需要进入管理员后端,你应该在Post下方看到Toys,在'Toys Categories'中创建你想要的类别。

答案 1 :(得分:1)

我知道我迟到了,但这可能有助于那些努力寻找添加WP自定义帖子类型的简单方法的人。

有一个很棒的库可以使用WordPress Post类型和分类法。

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

    在主题目录中
  1. 运行此命令。

    $ composer require azi/raskoh

  2. 将composer autoloader包含在主题functions.php

    require_once "vendor/autoloader.php";

  3. 在require之后添加此代码以注册帖子类型

  4. `

    $toy = new Raskoh\PostType("Toy");
    
    $toy->register();
    

    Raskoh (图书馆)将负责其余部分。

    这是图书馆

    Github