下面我将为你详细解释 WordPress 的信息分类体系,并提供从基础到高级的模板代码示例,帮助你灵活地在任何主题中调用和显示分类信息。

wordpress 信息分类模板
(图片来源网络,侵删)

核心概念:分类 vs. 标签

在开始写代码之前,必须理解这两个概念的区别。

特性 分类
层级关系 有层级,可以创建父子分类,形成树状结构(电脑 -> 笔记本 -> 游戏本)。 无层级,所有标签都是平级的,没有从属关系。
使用目的 进行宏观、系统的分类,它决定了文章的主要归属。 进行微观、灵活的标记,它描述了文章的具体细节、关键词或相关概念。
数量限制 一篇文章通常只属于一个分类(除非在后台设置允许多个)。 一篇文章可以有任意多个
默认显示 在主题的侧边栏、归档页面中通常有专门的显示区域。 通常显示在文章底部,作为相关文章或文章内链接的依据。

简单比喻:

  • 分类 就像一本书的目录(第一章、第二章...)。
  • 就像书中的关键词索引(“PHP”、“WordPress”、“前端开发”)。

基础模板代码调用

这些代码通常用在 archive.php(归档页)、category.php(分类页)、single.php(文章页)或 sidebar.php(侧边栏)中。

获取当前分类信息

当你在一个分类页面(如 category-5.php)时,可以使用 get_queried_object() 来获取当前分类的对象。

wordpress 信息分类模板
(图片来源网络,侵删)
<?php
// 获取当前分类对象
$current_category = get_queried_object();
if ( $current_category ) {
    // 获取分类ID
    $category_id = $current_category->term_id;
    // 获取分类名称
    $category_name = $current_category->name;
    // 获取分类链接
    $category_link = get_category_link( $category_id );
    // 获取分类描述
    $category_description = $current_category->description;
    echo "<h1>当前分类: " . esc_html( $category_name ) . "</h1>";
    echo "<p>描述: " . esc_html( $category_description ) . "</p>";
    echo "<a href='" . esc_url( $category_link ) . "'>查看分类</a>";
}
?>

获取文章所属的分类信息

single.phpthe_loop 循环中,使用 get_the_category() 来获取一篇文章的所有分类。

<?php
// 获取当前文章的所有分类对象数组
$categories = get_the_category();
if ( ! empty( $categories ) ) {
    echo "<p>本文分类: ";
    foreach ( $categories as $category ) {
        // 分类链接
        $category_link = get_category_link( $category->term_id );
        // 输出分类链接和名称
        echo '<a href="' . esc_url( $category_link ) . '">' . esc_html( $category->name ) . '</a> ';
    }
    echo "</p>";
}
?>

获取文章的所有标签

single.php 中,使用 get_the_tags() 来获取一篇文章的所有标签。

<?php
$tags = get_the_tags();
if ( $tags ) {
    echo "<p>本文标签: ";
    foreach ( $tags as $tag ) {
        // 标签链接
        $tag_link = get_tag_link( $tag->term_id );
        // 输出标签链接和名称
        echo '<a href="' . esc_url( $tag_link ) . '">' . esc_html( $tag->name ) . '</a> ';
    }
    echo "</p>";
} else {
    echo "<p>暂无标签。</p>";
}
?>

进阶模板代码示例

示例1:在侧边栏显示分类列表

这是最常见的需求,使用 wp_list_categories() 函数可以轻松实现。

<h3>文章分类</h3>
<ul>
    <?php
    // wp_list_categories() 函数参数
    $args = array(
        'orderby'    => 'name',  // 按名称排序
        'order'      => 'ASC',   // 升序
        'show_count' => true,    // 显示分类下的文章数量
        'title_li'   => __( '' ), // 不显示列表标题(因为我们已经有了h3)
        'hide_empty' => true,    // 不显示没有文章的分类
    );
    wp_list_categories( $args );
    ?>
</ul>

常用参数说明:

  • orderby: name (名称), count (文章数量), term_id (ID), slug (别名)
  • order: ASC (升序), DESC (降序)
  • show_count: true / false (是否显示文章数)
  • hide_empty: true / false (是否隐藏空分类)
  • child_of: 显示指定分类的子分类 ( child_of => 5)
  • exclude: 排除某些分类 ( exclude => 4,7)
  • include: 只包含某些分类 ( include => 5,9,23)

示例2:在文章页显示所有分类的链接

<?php
$categories = get_the_category();
$separator = ' | '; // 分类之间的分隔符
$output = '';
if ( $categories ) {
    foreach( $categories as $category ) {
        $output .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">';
        $output .= esc_html( $category->name ) . '</a>' . $separator;
    }
    echo trim( $output, $separator ); // 去掉最后一个分隔符
}
?>

示例3:创建一个“文章归档”页面

这个页面会按月份列出所有文章,并显示每个月份下的分类。

创建页面模板 在你的主题文件夹下创建一个新文件,archive-page.php,并在顶部添加以下注释:

<?php
/*
Template Name: 文章归档
*/
?>

编写模板代码archive-page.php 中,使用 get_terms() 函数来获取所有分类,然后遍历每个分类,再获取该分类下的文章。

<?php
get_header(); // 加载头部模板
// 获取所有分类,按名称排序
$categories = get_terms( 'category', array(
    'orderby'    => 'name',
    'hide_empty' => true,
) );
if ( $categories ) {
    echo '<div class="archive-container">';
    foreach ( $categories as $category ) {
        // 获取当前分类的ID
        $category_id = $category->term_id;
        // 获取该分类下的文章
        $args = array(
            'cat'              => $category_id,
            'posts_per_page'   => -1, // 显示该分类下所有文章
        );
        $category_posts = new WP_Query( $args );
        if ( $category_posts->have_posts() ) {
            echo '<h2><a href="' . get_category_link( $category_id ) . '">' . $category->name . '</a></h2>';
            echo '<ul>';
            while ( $category_posts->have_posts() ) {
                $category_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
        }
        // 重置查询,避免影响后续循环
        wp_reset_postdata();
    }
    echo '</div>';
} else {
    echo '<p>没有找到任何分类。</p>';
}
get_footer(); // 加载底部模板
?>

创建自定义分类法

除了默认的“分类”和“标签”,你还可以为特定内容创建自己的分类法,为“产品”创建一个“品牌”分类法。

functions.php 中注册自定义分类法

function my_custom_taxonomies() {
    // 为 'post' 类型创建一个名为 'product_brand' 的分类法
    register_taxonomy(
        'product_brand',          // 分类法名称 (小写,下划线)
        'post',                   // 应用的文章类型
        array(
            'label'             => __( '品牌' ), // 后台显示的名称
            'rewrite'          => array( 'slug' => 'brand' ), // 前台链接别名
            'hierarchical'      => true, // 是否有层级,像分类一样
            'show_admin_column' => true, // 在文章列表后台显示该列
            'query_var'         => true, // 是否支持查询
        )
    );
}
add_action( 'init', 'my_custom_taxonomies', 0 );

在模板中调用自定义分类法

注册后,你就可以像使用默认分类一样使用它了。

// 获取当前文章的所有 'product_brand'
$brands = get_the_terms( get_the_ID(), 'product_brand' );
if ( $brands && ! is_wp_error( $brands ) ) {
    echo "<p>品牌: ";
    foreach ( $brands as $brand ) {
        $brand_link = get_term_link( $brand );
        echo '<a href="' . esc_url( $brand_link ) . '">' . esc_html( $brand->name ) . '</a> ';
    }
    echo "</p>";
}
  • 基础get_the_category(), get_the_tags(), get_queried_object() 是获取分类信息的核心函数。
  • 列表wp_list_categories()wp_list_tags() 是生成分类/标签列表的便捷函数。
  • 高级get_terms()WP_Query 的结合使用,可以实现非常灵活和复杂的归档页面。
  • 扩展:通过 register_taxonomy() 可以创建完全自定义的分类体系,让你的 WordPress 网站结构更清晰、功能更强大。

希望这份详细的指南能帮助你掌握 WordPress 的信息分类模板!