在WordPress中创建自定义文章类型和自定义分类法

3
我需要为我销售的玩具建立一个自定义文章类型。我想创建的自定义文章类型是“玩具”。我想要它们有分类/标签,以便我以后可以进行排序。我现在想创建的标签是“浴室玩具”,“磁铁玩具”,“悠悠球”和“发光玩具”。
我认为如果我能观察代码,我可以尝试分析它,然后稍后复制它。
这是我一直在尝试遵循的教程。但是我仍然不清楚如何添加分类或标签。
我将这些功能添加到我的子主题的functions.php中,并且我正在使用WordPress 3.3.1。
2个回答

5

您希望在functions.php文件中使用register_taxonomy()register_post_type()函数定义自己的分类法和自定义文章类型。

以下是一个示例,供参考:

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

然后您需要进入管理员后台,在“帖子”下方应该会看到“玩具”,在“玩具分类”中创建所需的分类。


1
我知道我有些迟到,但这可能会帮助那些在寻找添加WP自定义文章类型的简单方法方面遇到困难的人。 有一个很棒的库可用于处理WordPress文章类型和分类法。 按照以下步骤操作,将使您的生活更简单。 1. 在您的主题目录中运行此命令。 $ composer require azi/raskoh 2. 将composer自动加载器包含在您的主题functions.php文件中。 require_once "vendor/autoloader.php"; 3. 在require之后添加此代码以注册文章类型。

`

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

$toy->register();

Raskoh(库)将处理其余部分。 这是该库:

Github


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接