当前位置:

WordPress后台添加产品自定义分类法

默认情况下, wordpress后台只有文章和页面二种分类方法,我们可以自己创建自定义分类法。下面以创建“产品”这个分类为例,介绍一下创建方法。

第一步:在functions.php里添加创建分类法函数;

function custom_post_pro() {
$labels = array(
'name' => _x( '产品', 'post type general name' ),
'singular_name' => _x( '产品', 'post type singular name' ),
'add_new' => _x( '发布产品', 'book' ),
'add_new_item' => __( '发布产品' ),
'edit_item' => __( '编辑产品' ),
'new_item' => __( '新产品' ),
'all_items' => __( '所有产品' ),
'view_item' => __( '查看产品' ),
'search_items' => __( '搜索产品' ),
'not_found' => __( '暂无产品' ),
'not_found_in_trash' => __( '暂无产品' ),
'parent_item_colon' => '',
'menu_name' => '产品'
);
$args = array(
'labels' => $labels,
'description' => '保存产品数据',
'public' => true,
'menu_position' => 3,
'show_in_rest' => true, // 确保此参数为true
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','tags' ),
'has_archive' => true,
);
register_post_type( 'pro', $args );
}
add_action( 'init', 'custom_post_pro' );
//Custom Taxonomies (pro Categories)
function taxonomies_pro() {
$labels = array(
'name' => _x( '产品分类', 'taxonomy general name' ),
'singular_name' => _x( '产品分类', 'taxonomy singular name' ),
'search_items' => __( '搜索产品分类' ),
'all_items' => __( '产品分类' ),
'parent_item' => __( '产品父分类' ),
'parent_item_colon' => __( '产品分类:' ),
'edit_item' => __( '产品分类修改' ),
'update_item' => __( '产品分类更新' ),
'add_new_item' => __( '新增产品分类' ),
'new_item_name' => __( '新产品分类' ),
'menu_name' => __( '产品分类' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'pro_cat', 'pro', $args );
}
add_action( 'init', 'taxonomies_pro', 0 );

// 注册标签型分类法(产品标签)
function register_product_tags() {
$labels = array(
'name' => '产品标签',
'singular_name' => '产品标签项',
'menu_name' => '产品标签'
);
$args = array(
'hierarchical' => false, // 关键:设置为非层级结构
'show_ui' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'product-tag')
);
register_taxonomy('product_tag', array('pro'), $args);
}

add_action('init', 'register_product_tags');

第二步:对接后台开发文件:config-example.php

分类的判断用:'pro_cat','product_tag'

如:

$taxonomy_cof = array('category','post_tag','pro_cat','product_tag');

文章的判断用:'pro'

如:

$tab_conf = array(
'title' => '产品信息填写',
'id'=>'tab_box',
'page'=>array('pro'),
'context'=>'normal',
'priority'=>'low',
'tab'=>true
);

第三步:面包屑导航代码的替换:

// 自定义面包屑导航函数
function custom_breadcrumbs() {
// 设置分隔符和首页标签
$delimiter = '<span class="sep">/</span>';
$home = '首页';

// 开始输出面包屑
echo '<div class="breadcrumb">';
echo '<a href="' . get_home_url() . '">' . $home . '</a> ' . $delimiter . ' ';

if (is_category() || is_tax('product_category')) {
// 分类/自定义分类法归档页
$term = get_queried_object();
$parents = get_ancestors($term->term_id, 'product_category');
foreach (array_reverse($parents) as $parent_id) {
$parent = get_term($parent_id, 'product_category');
echo '<a href="' . get_term_link($parent) . '">' . $parent->name . '</a> ' . $delimiter . ' ';
}
echo '<span class="current">' . $term->name . '</span>';
}
elseif (is_single() && get_post_type() === 'post') {
// 默认文章类型文章页
$categories = get_the_category();
if (!empty($categories)) {
$cat = $categories[0];
$parents = get_ancestors($cat->term_id, 'category');
foreach (array_reverse($parents) as $parent_id) {
$parent = get_category($parent_id);
echo '<a href="' . get_category_link($parent_id) . '">' . $parent->name . '</a> ' . $delimiter . ' ';
}
echo '<a href="' . get_category_link($cat->term_id) . '">' . $cat->name . '</a> ' . $delimiter . ' ';
}
echo '<span class="current">' . get_the_title() . '</span>';
}
elseif (is_single() && get_post_type() === 'page') {
// 页面类型
$parents = get_post_ancestors(get_the_ID());
if ($parents) {
foreach (array_reverse($parents) as $parent_id) {
echo '<a href="' . get_permalink($parent_id) . '">' . get_the_title($parent_id) . '</a> ' . $delimiter . ' ';
}
}
echo '<span class="current">' . get_the_title() . '</span>';
}
elseif (is_tag() || is_tax('product_tag')) {
// 标签/自定义标签归档页
$term = get_queried_object();
echo '<span class="current">标签: ' . $term->name . '</span>';
}

echo '</div>';
}

// 在模板中调用面包屑
function display_breadcrumbs() {
if (function_exists('custom_breadcrumbs')) {
custom_breadcrumbs();
}
}

这样就可以创建一个自定义分类法了,并且正常对接网站的开发代码。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注