WordPress自定义文章类型分页异常:空第四页问题求助
这个空分页的问题核心在于你自定义的WP_Query和分页函数依赖的全局$wp_query不匹配,另外还有几处参数拼写错误也可能干扰查询逻辑,下面一步步帮你修复:
1. 先修正WP_Query的参数拼写错误
在archive-projects.php的两处WP_Query配置里,有两个低级拼写错误:
'order-by'应该改为'orderby'(少了字母b,WordPress的查询参数是orderby)'order' => 'des'应该改为'order' => 'desc'(完整的降序参数是desc)
这两个错误会导致你的文章排序逻辑异常,先把它们修正。
2. 让分页函数使用自定义查询的分页数据
你的分页函数aa_numeric_posts_nav()默认读取全局$wp_query的分页信息,但桌面端你用的是自定义的$the_query,这就导致分页逻辑计算的是主查询(而非projects自定义文章类型)的总页数,自然会出现错误的空页面。
推荐采用下面更规范的修复方式:
方式一:修改分页函数支持传入自定义查询对象
把你的aa_numeric_posts_nav()函数改成可接收WP_Query对象的版本,这样就能在调用时传入你的自定义查询:
function aa_numeric_posts_nav($query = null) { if( is_singular() ) return; // 优先使用传入的自定义查询,否则用全局wp_query $wp_query = $query ? $query : $GLOBALS['wp_query']; /** Stop execution if there's only 1 page */ if( $wp_query->max_num_pages <= 1 ) return; $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = intval( $wp_query->max_num_pages ); /** Add current page to the array */ $links = []; if ( $paged >= 1 ) $links[] = $paged; /** Add the pages around the current page to the array */ if ( $paged >= 3 ) { $links[] = $paged - 1; $links[] = $paged - 2; } // 修复原函数的逻辑漏洞:判断页码不超过总页数再添加 if ( ( $paged + 2 ) <= $max ) { $links[] = $paged + 1; $links[] = $paged + 2; } echo '<div id="navigation"><ul class="pagination numbered-pagination">' . "\n"; /** Previous Post Link */ if ( get_previous_posts_link('', $wp_query) ) printf( '<li class="nav-prev">%s</li>' . "\n", get_previous_posts_link('', $wp_query) ); /** Link to first page, plus ellipses if necessary */ if ( ! in_array( 1, $links ) ) { $class = 1 == $paged ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' ); if ( ! in_array( 2, $links ) ) echo '<li>…</li>'; } /** Link to current page, plus 2 pages in either direction if necessary */ sort( $links ); foreach ( (array) $links as $link ) { $class = $paged == $link ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link ); } /** Link to last page, plus ellipses if necessary */ if ( ! in_array( $max, $links ) ) { if ( ! in_array( $max - 1, $links ) ) echo '<li>…</li>' . "\n"; $class = $paged == $max ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max ); } /** Next Post Link */ if ( get_next_posts_link('', $wp_query->max_num_pages, $wp_query) ) printf( '<li class="nav-next">%s</li>' . "\n", get_next_posts_link('', $wp_query->max_num_pages, $wp_query) ); echo '</ul></div>' . "\n"; }
注:我还修复了原函数的一个逻辑漏洞——之前的代码没有判断$paged + 2是否超过总页数就直接添加页码,这也是生成多余空页面的原因之一。
然后在archive-projects.php里调用分页函数时,传入你的自定义查询对象:
<?php aa_numeric_posts_nav($the_query); ?>
方式二:临时替换全局$wp_query(快速生效但不推荐)
如果你不想修改分页函数,可以在桌面端的WP_Query之后,把自定义查询赋值给全局$wp_query,但这种方式可能影响其他依赖主查询的功能,谨慎使用:
<?php $args = array( 'post_type' => 'projects', 'orderby' => 'date', 'order' => 'desc', 'paged' => $paged, 'posts_per_page' => 6 ); $the_query = new WP_Query( $args ); // 替换全局wp_query global $wp_query; $wp_query = $the_query; if($the_query->have_posts() ) : // ... 后续代码不变 ?>
3. 验证修复效果
完成以上修改后,刷新桌面端归档页面,分页应该会正确显示3页,第四页的空页面也会消失。另外建议你确认下projects类型的文章总数:如果总数是18篇(3×6),总页数就是3页;如果总数在19-23之间,总页数确实是4页,但此时第四页会有内容,不会为空。
内容的提问来源于stack exchange,提问作者Leanne

