场景
在使用分页钩子方面,我试图了解我的循环哪里出了问题。我使用的主题底带,带底带子,我没有编辑预构建的钩子,可以找到这里。
我在下面列出的循环中使用了这个钩子,它驻留在一个页面模板文件中。但是,分页钩子没有被输出?然而,循环的其余部分工作得非常好。
让我们看看,我在我的index.php文件中使用了完全相同的结构。而且它工作得很好。这让我相信,我可能需要宣布一些事情,只是不确定这可能是什么。
问题
我想知道在使用页面模板和new WP_Query();时,是否需要声明任何全局值才能使$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;工作?
代码中的问题
以供参考--我已经包括了我的循环,尽管我非常肯定它是可以的。
<div class="bg-light">
<div class="container space-top-3 space-bottom-2">
<div class="row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'perm' => 'readable',
'show_post_views' => true,
'posts_per_page' => 9,
'paged' => $paged
);
$latestArticles = new WP_Query( $args );
if( $latestArticles->have_posts() ):
/* Start the Loop */
while( $latestArticles->have_posts() ) : $latestArticles->the_post();
get_template_part( 'loop-templates/content-search', get_post_format() );
endwhile;
endif;?>
</div>
<div class="row mt-3">
<div class="col-auto">
<!-- The pagination component (currently not being output. not sure why.) -->
<?php understrap_pagination(); ?>
</div>
</div>
<?php wp_reset_query(); ?>
<?php else : ?>
<!-- do nothing -->
<?php endif; ?>
</div>
</div>谢谢你的帮助,提前。
Edit-7/11/19:
尝试将$latestArticles->max_num_pages作为这样的understrap_pagination($latestArticles->max_num_pages);传递给understrap_pagination();。-还是没有运气:
发布于 2019-11-07 18:32:21
分页函数依赖于$wp_query->max_num_pages (即主查询的最大页数),因此该函数将无法处理自定义查询:
function understrap_pagination( $args = array(), $class = 'pagination' ) {
if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) {
return;
}
...
}虽然您的方法确实有效,但是更改全局$wp_query对象确实不是一个好主意,因为这样做与使用根本不建议使用的query_posts()本质上是一样的--您可以通过检查docs 这里了解原因。
但是,如果您不想编辑分页函数(或创建自己的),那么要使分页工作于您的自定义查询,一个风险较小的解决办法是,只临时更改-和(- $wp_query->max_num_pages )。
$orig_max_num_pages = $wp_query->max_num_pages; // backup
// Change it just for the pagination.
// and $latestArticles is your custom WP_Query instance.
$wp_query->max_num_pages = $latestArticles->max_num_pages;
$paged = max( get_query_var( 'paged' ), get_query_var( 'page' ), 1 );
understrap_pagination( [
'current' => $paged,
] );
$wp_query->max_num_pages = $orig_max_num_pages; // restore更好的实践:复制/编辑分页功能。
由于您使用的是子主题,而且该承重带使用locate_template()加载文件(inc/pagination.php),该文件定义了分页函数,因此您只需将文件复制到子主题(your-theme/inc/pagination.php)并编辑函数,以便它可以处理自定义的WP_Query查询。或者因为函数是可插拔的(即用if ( ! function_exists() )块定义的),所以您可以将代码复制到子主题的functions.php文件中。
paged对page
然而,paged和page都是当前页面的编号:
paged用于基于归档的请求/URL,如类别归档和搜索结果页面。在这些请求中,默认情况下不设置page。还应该注意的是,paged与is_paged()一起使用。page与单一的请求/URL一起使用,例如单个Post和pages。在这些请求中,默认情况下不设置paged。Codex建议采用以下方法来获得单个请求/URL上的适当页码:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }或更简单的版本:$paged = max( get_query_var( 'paged' ), get_query_var( 'page' ), 1 );
注:该法典文章提及静态首页,但该信息也适用于任何网页/帖子。
最后但并非最不重要的是…使用辅助WP_Query调用(如您的$latestArticles = new WP_Query( $args )),只需调用wp_reset_postdata()而不需要调用wp_reset_query()。:)
发布于 2019-11-07 12:13:07
因此,我能够通过CSS-Tricks定位这个帖子这里,它概述了一个方法,该方法对当前的$wp_query变量进行核弹,并将其替换为WP_Query();的一个新实例。
我的循环现在像预期的那样工作,然而,我的印象是$wp_query是保留的,这不对吗?使用这种方法安全吗?
我的新循环看起来是这样的;
<div class="bg-light">
<div class="container space-top-3 space-bottom-2">
<div class="row">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'perm' => 'readable',
'show_post_views' => true,
'posts_per_page' => $posts_per_page,
'paged' => $paged
); ?>
<?php $wp_query->query($args); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop-templates/content-search', get_post_format() ); ?>
<?php endwhile; ?>
</div>
<div class="row mt-3">
<div class="col-auto">
<!-- The pagination component -->
<?php understrap_pagination(); ?>
</div>
</div>
<?php else : ?>
<!-- do nothing -->
<?php endif; ?>
<?php $wp_query = null;
$wp_query = $temp; // Reset ?>
</div>
</div>如果有人知道更好的方法,我会感兴趣的。在此之前,非常感谢您。-B。
https://stackoverflow.com/questions/58709835
复制相似问题