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

问题描述 投票:2回答:2

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

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

这是我一直试图遵循的the tutorial。但它仍然让我感到困惑,如何添加分类法或标签。

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

wordpress custom-post-type
2个回答
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
投票

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

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

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

  1. 在您的主题目录中运行此命令。 $ composer require azi/raskoh
  2. 包括作曲家自动加载器到你的主题functions.php require_once "vendor/autoloader.php";
  3. 在require之后添加此代码以注册帖子类型

`

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

$toy->register();

Raskoh(图书馆)将负责其余的工作。这是图书馆

Github

© www.soinside.com 2019 - 2024. All rights reserved.