You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按术语名称筛选自定义文章类型并实现分页功能

需求说明

已实现自定义文章类型(documentation)的循环展示,但当前代码按每页术语(document-type分类)数量分页,需改为按每页文章数量分页,同时适配对应分页逻辑,寻求简洁高效的实现方式。

现有代码如下:

// Get all the categories
$categories = get_terms( 'document-type' );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$post_type = 'documentation';
$taxonomy  = 'document-type';

// count the number of terms for correct pagination
$term_count = get_terms( array (
    'taxonomy' => $taxonomy,
    'fields'   => 'count',
) );

// define the number of terms per page
$terms_per_page = 2;

// find out the number of pages to use in pagination
$max_num_pages = ceil( $term_count / $terms_per_page );

// get the page number from URL query
$current_page = get_query_var( 'paged', 1 );

// calculate offset
$offset = ( $terms_per_page * $current_page ) - $terms_per_page;

// get all taxonomy terms
$terms = get_terms( array (
    'taxonomy' => $taxonomy,
    'order'    => 'ASC',
    'orderby'  => 'name',
    'number'   => $terms_per_page,
    'offset'   => $offset,
) );

// cycle through taxonomy terms
foreach ( $terms as $term ) {

    // cycle through posts having this term
    $items = get_posts( array (
        'post_type'   => $post_type,
        'tax_query'   => array(
            array(
                'taxonomy' => $taxonomy,
                'terms'    => $term->term_id,
            ),
        ),
        'posts_per_page' => -1, // different from WP_Query (see Code Ref)
    ) );

    // essential, see comments inside foreach() loop
    global $post;

    foreach ( $items as $item ) {
     the_title();
    }
    wp_reset_postdata(); // moved outside the foreach() loop


}
实现方案

直接使用WP_Query查询所有documentation类型的文章,借助WordPress原生分页逻辑完成需求,无需多次嵌套查询,性能更优:

// 获取当前分页页码,默认第1页
$paged = get_query_var('paged', 1);
// 定义每页显示的文章数量
$posts_per_page = 10;

// 构建查询参数
$docs_query = new WP_Query([
    'post_type'      => 'documentation',
    'post_status'    => 'publish',
    'paged'          => $paged,
    'posts_per_page' => $posts_per_page,
    'orderby'        => 'title',
    'order'          => 'ASC'
]);

// 循环输出文章内容
if ($docs_query->have_posts()) {
    while ($docs_query->have_posts()) {
        $docs_query->the_post();
        // 输出文章标题,可根据需求扩展内容(如摘要、链接等)
        the_title('<h3 class="doc-title">', '</h3>');
    }

    // 输出分页导航
    echo paginate_links([
        'total'   => $docs_query->max_num_pages,
        'current' => $paged,
        'prev_text' => '上一页',
        'next_text' => '下一页'
    ]);

    // 重置查询
    wp_reset_postdata();
} else {
    echo '<p>暂无文档内容</p>';
}

核心优化点

  • 取消原代码中“查分类→循环分类查文章”的冗余逻辑,一次WP_Query直接获取分页后的目标文章,减少数据库查询次数
  • 利用WordPress原生的paged参数和paginate_links()自动处理分页计算,无需手动计算偏移量、总页数
  • 若需保留按分类分组展示的需求,可在循环中判断文章所属分类,实现分类标题的分组输出:
$current_term_id = 0;

if ($docs_query->have_posts()) {
    while ($docs_query->have_posts()) {
        $docs_query->the_post();
        
        // 获取文章所属的document-type分类
        $terms = get_the_terms(get_the_ID(), 'document-type');
        if ($terms && !is_wp_error($terms)) {
            $current_term = reset($terms);
            // 分类变化时输出分类标题
            if ($current_term->term_id !== $current_term_id) {
                echo '<h2 class="doc-category">' . esc_html($current_term->name) . '</h2>';
                $current_term_id = $current_term->term_id;
            }
        }
        
        // 输出文章标题
        the_title('<h3 class="doc-title">', '</h3>');
    }

    // 分页导航
    echo paginate_links([
        'total'   => $docs_query->max_num_pages,
        'current' => $paged,
        'prev_text' => '上一页',
        'next_text' => '下一页'
    ]);

    wp_reset_postdata();
}

内容的提问来源于stack exchange,提问作者Thenewebdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 01:31:21