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

WordPress首页分页功能异常,所有分页链接均展示相同文章如何解决

问题原因及修正方案

你的代码存在4个直接导致分页内容重复的问题,以下是逐一说明和修复后的代码:

问题列表

  • 混用offset和paged参数:WordPress中固定设置offset会覆盖paged的分页计算逻辑,导致每一页都是从第4篇文章开始取10篇,内容完全相同。如果需要跳过最新3篇文章,需要手动计算对应分页的偏移量。
  • 分页变量获取错误:首页的分页查询变量是paged,你用的page仅适用于单篇文章/页面的内部分页场景,这会导致代码永远读取到的页码为1,永远查询第一页内容。
  • 分页函数未关联自定义查询:paginate_links()默认读取全局主查询的分页参数,你使用自定义的$blogs查询时需要传入总页数等参数,否则生成的分页和你的文章查询不匹配。
  • 重置函数使用错误:自定义WP_Query查询结束后应该使用wp_reset_postdata()重置全局文章变量,你用的wp_reset_query()是用于重置主查询的,此处适用场景错误。

修正后代码

<?php
    // 正确获取分页参数
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    // 跳过最新3篇的基础偏移量
    $base_offset = 3;
    // 每页展示数量
    $posts_per_page = 10;
    // 计算当前页实际偏移量
    $current_offset = $base_offset + ( ($paged - 1) * $posts_per_page );
    
    $args = array(
        'post_type'=>'post', 
        'posts_per_page'=>$posts_per_page, 
        'orderby'=>'date', 
        'order'=>'DESC', 
        'offset'=>$current_offset, 
        'paged'=>$paged
    );
    $blogs = new WP_Query($args);
    if($blogs->have_posts()):
        while($blogs->have_posts()): $blogs->the_post();
?>

    <div class="col-md-6">
        <div class="single-post">
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <img src="<?php the_post_thumbnail_url('blog-thumbnail'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
                </a>
            </div>
            <div class="content">
                <div class="category">
                    <?php
                    $categories = get_the_category();
                    foreach ($categories as $key => $category) {
                        echo '<a href="' . get_category_link($category->term_id). '">'. $category->name. '</a>';
                    }
                    ?>
                </div>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="metadata">
                    <span class="date"><?php echo get_the_date(); ?></span>
                    <span class="author"><?php echo get_the_author(); ?></span>
                </div>
                <div class="excerpt">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        </div>
    </div>

<?php
        endwhile;
        // 传入自定义查询的总页数参数,生成正确分页链接
        echo '<div class="paginate-wrap">'. paginate_links(array(
            'total' => $blogs->max_num_pages
        )) . '</div>';
    endif;
    // 正确重置文章全局变量
    wp_reset_postdata();
?>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 12:45:03