WordPress首页分页失效:URL变更但内容无更新
WordPress首页分页内容不更新问题修复
问题分析
点击分页链接后URL显示/page/3,但页面仍展示第1页内容,分页按钮始终高亮第1页,核心原因:
- 自定义模板的静态首页,WordPress会用
page作为分页参数,而非归档页使用的paged - 原代码混淆了两种参数的获取逻辑,导致始终读取到第1页
修复步骤
1. 修正页码获取逻辑
优先读取静态首页专用的page参数,同时兼容归档页的paged参数,确保不同场景下都能正确获取当前页码。
2. 修正分页链接生成逻辑
移除paginate_links中多余的query参数,避免干扰链接生成;同时直接使用已获取的$paged作为当前页码,保证高亮状态正确。
完整修复后代码
<?php /** * Genesis Sample. * * 为Genesis Sample主题添加首页模板 * * Template Name: Home * * @package Genesis Sample * @author StudioPress * @license GPL-2.0-or-later * @link https://www.studiopress.com/ */ get_header(); // 静态首页用page参数,归档页用paged参数,做兼容处理 $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : $paged; $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 9, 'paged' => $paged, ); $wp_query = new WP_Query( $args ); ?> <div class="body-container"> <div class="blogs-banner"> <?php $image = get_field('image'); if (!empty($image)) : ?> <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" class="img-main" /> <?php endif; ?> </div> <div class="about-container"> <div class="blog-list"> <div class="row-same-height" id="postContainer" data-type="post" data-count="<?php echo $wp_query->found_posts; ?>"> <?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <a href="<?php the_permalink(); ?>" class="link-post"> <div class="col-same-height"> <div class="content"> <div class="TopArticleCard-Container"> <div class="img-container"> <?php $thumbnail = get_post_meta(get_the_id(), 'large_img', true); if ( $thumbnail ) { ?> <img src="<?php echo esc_url($thumbnail); ?>" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" loading="lazy"/> <?php } else if ( has_post_thumbnail() ) { the_post_thumbnail('large', array( 'title' => the_title_attribute( array( 'echo' => false ) ), 'alt' => the_title_attribute( array( 'echo' => false ) ) )); ?> <?php } else { ?> <img src="<?php echo esc_url(get_site_url()); ?>/wp-content/uploads/2020/09/default.jpg" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" loading="lazy"/> <?php } ?> </div> <div class="article-description"> <div class="article-tag"> <?php $cat = get_the_category(); echo esc_html($cat[0]->cat_name); ?> </div> <div class="content"> <div class="article-title"> <?php the_title(); ?> </div> </div> </div> </div> </div> </div> </a> <?php endwhile; endif; wp_reset_postdata(); ?> </div> <nav class="pagination-nav"> <ul class="pagination"> <?php if ( $wp_query->max_num_pages > 1 ) : $pagination_args = array( 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ), 'format' => '/page/%#%', 'current' => $paged, 'total' => $wp_query->max_num_pages, 'prev_text' => 'Prev', 'next_text' => 'Next', ); echo paginate_links( $pagination_args ); endif; ?> </ul> </nav> </div> </div> </div> </div> <?php get_footer();
额外优化点
- 修复文章链接为空的问题:将
<a href=""改为<a href="<?php the_permalink(); ?>" - 添加
esc_url、esc_html等安全函数,避免XSS风险 - 优化标题属性的获取方式,使用
the_title_attribute更规范
内容的提问来源于stack exchange,提问作者mobile buddy
相关产品推荐
相关产品推荐

