引言
在上一节教程中,我们学习了如何创建自定义文章类型(Custom Post Types),如“产品”类型。但是,单独的文章类型可能还不够灵活,我们可能需要进一步对这些内容进行分类,比如“产品”可以按“品牌”或“类别”进行分类。
WordPress 提供了 自定义分类法(Custom Taxonomies),允许开发者定义自己的分类结构,以更灵活地管理网站内容。本教程将介绍如何创建并使用自定义分类法。
一、注册自定义分类法
1. 什么是分类法(Taxonomies)?
WordPress 自带的分类法包括:
- 分类(Categories):层级化的分类,如文章分类。
- 标签(Tags):非层级化的分类,如文章标签。
我们可以使用 register_taxonomy()
来创建新的分类法,并将其关联到自定义文章类型。
2. 在 functions.php
中注册分类法
我们将为“产品”文章类型添加“产品类别”分类,让不同类型的产品更容易管理。
打开主题的 functions.php
文件,并添加以下代码:
function create_product_taxonomies() {
$labels = array(
'name' => __('产品类别', 'text_domain'),
'singular_name' => __('产品类别', 'text_domain'),
'search_items' => __('搜索产品类别', 'text_domain'),
'all_items' => __('所有产品类别', 'text_domain'),
'parent_item' => __('父级类别', 'text_domain'),
'parent_item_colon' => __('父级类别:', 'text_domain'),
'edit_item' => __('编辑类别', 'text_domain'),
'update_item' => __('更新类别', 'text_domain'),
'add_new_item' => __('添加新类别', 'text_domain'),
'new_item_name' => __('新类别名称', 'text_domain'),
'menu_name' => __('产品类别', 'text_domain'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true, // 是否层级化(类似分类)
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product-category'),
);
register_taxonomy('product_category', array('product'), $args);
}
add_action('init', 'create_product_taxonomies');
3. 代码解析
$labels
:定义分类的后台显示名称。$args
:'hierarchical' => true
:允许分类嵌套(类似 WordPress 的“分类”)。'show_admin_column' => true
:在后台产品管理列表中显示分类信息。'rewrite' => array('slug' => 'product-category')
:设置分类的 URL 结构,例如yourdomain.com/product-category/类别名称/
。
register_taxonomy('product_category', array('product'), $args);
:- 第一个参数
'product_category'
是分类法的唯一标识符。 - 第二个参数
array('product')
关联到我们之前创建的product
文章类型。
- 第一个参数
保存文件并刷新 WordPress 后台,在“产品”管理菜单下,你会发现新的“产品类别”选项。
二、在前端显示分类法
创建分类法后,我们需要在前端模板中显示它们,以便用户可以按类别浏览产品。
1. 在 single-product.php
显示产品类别
打开 single-product.php
,并在合适的位置(比如 the_title()
下面)添加以下代码:
<?php
$terms = get_the_terms(get_the_ID(), 'product_category');
if ($terms && !is_wp_error($terms)) {
echo '<p>类别: ';
foreach ($terms as $term) {
echo '<a href="' . get_term_link($term) . '">' . esc_html($term->name) . '</a> ';
}
echo '</p>';
}
?>
2. 创建分类存档页面 taxonomy-product_category.php
当用户点击某个类别时,WordPress 会自动加载 taxonomy-分类法名称.php
作为存档页面。如果没有此模板,它将回退到 archive.php
。
在主题目录下创建 taxonomy-product_category.php
,并添加以下代码:
<?php get_header(); ?>
<div class="container">
<h1><?php single_term_title(); ?></h1>
<p><?php echo term_description(); ?></p>
<?php if (have_posts()) : ?>
<div class="product-list">
<?php while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="product-thumbnail">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('medium');
} ?>
</div>
<div class="product-excerpt"><?php the_excerpt(); ?></div>
</article>
<?php endwhile; ?>
</div>
<?php the_posts_pagination(); ?>
<?php else : ?>
<p>暂无产品。</p>
<?php endif; ?>
</div>
<?php get_footer(); ?>
3. 代码解析
single_term_title()
:获取当前分类的名称并显示。term_description()
:显示分类的描述(可在后台编辑分类时填写)。have_posts()
和the_post()
:循环显示属于该分类的所有产品文章。
现在,访问 yourdomain.com/product-category/类别名称/
,就能看到该类别下的所有产品。
三、在后台管理界面优化分类选择
默认情况下,在编辑产品时,分类选择框位于较下方。我们可以使用 add_meta_box()
让它更明显。
在 functions.php
添加以下代码:
function move_taxonomy_metabox() {
remove_meta_box('product_categorydiv', 'product', 'side');
add_meta_box('product_categorydiv', '产品类别', 'post_categories_meta_box', 'product', 'normal', 'high', array('taxonomy' => 'product_category'));
}
add_action('admin_menu', 'move_taxonomy_metabox');
这样,“产品类别”选择框会出现在编辑页面的更上方,更方便使用。
四、总结
在本教程中,我们学习了:
- 如何创建自定义分类法,并将其绑定到“产品”文章类型。
- 如何在前端显示分类,包括在
single-product.php
和taxonomy-product_category.php
中使用。 - 如何优化后台管理体验,让分类选择更直观。
通过自定义分类法,网站可以更有条理地组织内容,提高用户体验。在下一篇教程中,我们将介绍如何为自定义文章类型添加 自定义字段(Custom Fields),进一步扩展数据管理能力。