学做网站培训课程介绍

当前位置:

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();
}
}

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

发表评论

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

相关教程

  • 使用Wordpress 开发网站时,经常需要使用WP_Query来筛选出自己需要的内容,WP_Query功能非常强大,下面汇总了WP_Query常用的筛选语句。第一条:筛选
  • 使用Elementor创建专业的英文网站步骤如下:1、使用虚拟主机安装好WORDPRESS程序;2、安装Astra主题,后台---主题--安装新主题---Astra主题;3、
  • 默认情况下Wordpress后台只有文章和页面二种文章类型,如果我们建企业网站,所发布的产品与文章就会合在一起,不方便管理。怎么在wordpress后台菜单添
  • 我们安装WORDPRESS 插件时,插件需要加载用钩子挂载WordPress 自带的 jQuery库而非是通过硬编码的方式来加载,毕竟是为了避免多次加载 jQuery库。但在
  • wordpress默认情况下后台只支持mail函数方式设置发邮件,但是现在大多数虚拟主机是不支持mail函数的,因为会导致垃圾邮件的产生而服务器ip被大量列入
  • WordPress搜索页如果需要显示搜索结果的数量,一般是使用WordPress自带的函数$found_posts来显示。通常用法是:<?php echo $wp_query->found
  • 我们做网站时,如何将分类栏目里的置顶文章显示在最前面呢?然后再显示新发布的文章。怎么实现在调用最新文章列表中置顶文章靠前显示呢?代码如下:
  • WordPress网站后台上传了图片之后,每个图片都会有ID,我们在建网站时,可以通过图片ID来获取图片的路径。怎么通过图片附件的ID来获取图片的路径呢?
  • wordpress每增加一个分类目录,就会分配一个唯一的ID号。分类目录ID号相当于分类目录的身份证号,在网站制作过程中,我们需要经常使用到ID号。那么如
  • WordPress建好网站之后,往往由于服务器本身漏洞、网站后台使用弱密码登录等原因,造成网站被黑,植入了非正规的垃圾代码。造成网站跳转或者打不开。