gpt4 book ai didi

wordpress - 使用自定义分类法在 Wordpress 中创建自定义帖子类型

转载 作者:行者123 更新时间:2023-12-05 01:48:37 25 4
gpt4 key购买 nike

我需要为我销售的玩具构建自定义帖子类型。我要创建的自定义帖子类型是“玩具”。我希望它们有一个类别/标签,以便稍后对它们进行分类,我现在要创建的标签是“沐浴玩具”、“磁铁”、“溜溜球”和“夜光”。

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

这是 the tutorial我一直在努力遵循。但它仍然让我困惑如何添加分类法或标签。

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

最佳答案

您想使用 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 );
}

然后你需要去管理后台,你应该在帖子下面看到玩具,在'玩具类别'中创建你想要的类别。

关于wordpress - 使用自定义分类法在 Wordpress 中创建自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com