如何修复自定义循环的分页?
4 个回答
- 投票数
-
- 2015-11-09
我将此代码用于带有分页的自定义循环:
<?php if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page $paged = get_query_var('page'); } else { $paged = 1; } $custom_query_args = array( 'post_type' => 'post', 'posts_per_page' => get_option('posts_per_page'), 'paged' => $paged, 'post_status' => 'publish', 'ignore_sticky_posts' => true, //'category_name' => 'custom-cat', 'order' => 'DESC', // 'ASC' 'orderby' => 'date' // modified | title | name | ID | rand ); $custom_query = new WP_Query( $custom_query_args ); if ( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?> <article <?php post_class(); ?>> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small> <div><?php the_excerpt(); ?></div> </article> <?php endwhile; ?> <?php if ($custom_query->max_num_pages > 1) : // custom pagination ?> <?php $orig_query = $wp_query; // fix for pagination to work $wp_query = $custom_query; ?> <nav class="prev-next-posts"> <div class="prev-posts-link"> <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?> </div> <div class="next-posts-link"> <?php echo get_previous_posts_link( 'Newer Entries' ); ?> </div> </nav> <?php $wp_query = $orig_query; // fix for pagination to work ?> <?php endif; ?> <?php wp_reset_postdata(); // reset the query else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
来源:
I use this code for custom loop with pagination:
<?php if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page $paged = get_query_var('page'); } else { $paged = 1; } $custom_query_args = array( 'post_type' => 'post', 'posts_per_page' => get_option('posts_per_page'), 'paged' => $paged, 'post_status' => 'publish', 'ignore_sticky_posts' => true, //'category_name' => 'custom-cat', 'order' => 'DESC', // 'ASC' 'orderby' => 'date' // modified | title | name | ID | rand ); $custom_query = new WP_Query( $custom_query_args ); if ( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?> <article <?php post_class(); ?>> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small> <div><?php the_excerpt(); ?></div> </article> <?php endwhile; ?> <?php if ($custom_query->max_num_pages > 1) : // custom pagination ?> <?php $orig_query = $wp_query; // fix for pagination to work $wp_query = $custom_query; ?> <nav class="prev-next-posts"> <div class="prev-posts-link"> <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?> </div> <div class="next-posts-link"> <?php echo get_previous_posts_link( 'Newer Entries' ); ?> </div> </nav> <?php $wp_query = $orig_query; // fix for pagination to work ?> <?php endif; ?> <?php wp_reset_postdata(); // reset the query else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
Source:
-
- 2016-12-17
很棒,一如既往.作为对此的附录,请考虑以下情况:正在使用附加到Page的全局页面模板获取一些"介绍性文本",然后是要分页的子查询.
使用上面提到的paginate_links(),主要使用默认值,(并假设漂亮的永久链接已打开),您的分页链接将默认为
mysite.ca/page-slug/page/#
,这很可爱,但会由于WordPress不知道而引发404
错误有关该特定网址结构的信息,实际上将查找"page"的子页面,而该子页面是"page-slug"的子页面.这里的窍门是插入一个巧妙的重写规则,该规则仅适用于接受
/page/#/
结构并将其重写为一个查询字符串的特定"伪存档页面"页面段WordPress可以理解,即mysite.ca/?pagename=page-slug&paged=#
.注意pagename
和paged
而不是name
和page
(这确实使我感到悲伤的时光,在这里激发了这个答案! ).这是重定向规则:
add_rewrite_rule( "page-slug/page/([0-9]{1,})/?$", 'index.php?pagename=page-slug&paged=$matches[1]', "top" );
与往常一样,在更改重写规则时,请记住通过访问管理后端中的"设置">"永久链接"来清除永久链接.
如果有多个页面将以这种方式运行(例如,当处理多种自定义帖子类型时),则可能要避免为每个页面段创建新的重写规则.我们可以编写一个更通用的正则表达式,该正则表达式适用于您确定的任何页面信息.
下面是一种方法:
function wpse_120407_pseudo_archive_rewrite(){ // Add the slugs of the pages that are using a Global Template to simulate being an "archive" page $pseudo_archive_pages = array( "all-movies", "all-actors" ); $slug_clause = implode( "|", $pseudo_archive_pages ); add_rewrite_rule( "($slug_clause)/page/([0-9]{1,})/?$", 'index.php?pagename=$matches[1]&paged=$matches[2]', "top" ); } add_action( 'init', 'wpse_120407_pseudo_archive_rewrite' );
缺点/警告
这种方法的一个缺点是使Page slug硬编码,这使我有些不耐烦.如果管理员曾经更改过该伪归档页面的页面标签,那么您将敬酒-重写规则将不再匹配,您将获得可怕的404.
我不确定是否可以考虑此方法的解决方法,但是如果是全局页面模板以某种方式触发了重写规则,那就太好了.有一天,如果没有其他人能破解那个特殊的螺母,我可能会重新审视这个答案.
Awesome as always Chip. As an addendum to this, consider the situation whereby you are using a global page template attached to a Page for some "intro text" and it's followed by a subquery that you want to be paged.
Using paginate_links() as you mention above, with mostly defaults, (and assuming you have pretty permalinks turned on) your pagination links will default to
mysite.ca/page-slug/page/#
which is lovely but will throw404
errors because WordPress doesn't know about that particular URL structure and will actually look for a child page of "page" that's a child of "page-slug".The trick here is to insert a nifty rewrite rule that only applies to that particular "pseudo archive page" page slug that accepts the
/page/#/
structure and rewrites it to a query string that WordPress CAN understand, namelymysite.ca/?pagename=page-slug&paged=#
. Notepagename
andpaged
notname
andpage
(which caused me literally HOURS of grief, motivating this answer here!).Here's the redirect rule:
add_rewrite_rule( "page-slug/page/([0-9]{1,})/?$", 'index.php?pagename=page-slug&paged=$matches[1]', "top" );
As always, when changing rewrite rules, remember to flush your permalinks by visiting Settings > Permalinks in the Admin back-end.
If you have multiple pages that are going to behave this way (for example, when dealing with multiple custom post types), you might want to avoid creating a new rewrite rule for each page slug. We can write a more generic regular expression that works for any page slug you identify.
One approach is below:
function wpse_120407_pseudo_archive_rewrite(){ // Add the slugs of the pages that are using a Global Template to simulate being an "archive" page $pseudo_archive_pages = array( "all-movies", "all-actors" ); $slug_clause = implode( "|", $pseudo_archive_pages ); add_rewrite_rule( "($slug_clause)/page/([0-9]{1,})/?$", 'index.php?pagename=$matches[1]&paged=$matches[2]', "top" ); } add_action( 'init', 'wpse_120407_pseudo_archive_rewrite' );
Disadvantages / Caveats
One disadvantage of this approach that makes me puke in my mouth a little is the hard-coding of the Page slug. If an admin ever changes the page slug of that pseudo-archive page, you're toast - the rewrite rule will no longer match and you'll get the dreaded 404.
I'm not sure I can think of a workaround for this method, but it would be nice if it were the global page template that somehow triggered the rewrite rule. Some day I may revisit this answer if no one else has cracked that particular nut.
-
您可以挂钩保存,检查页面是否在元键" _wp_page_template"下具有存档模板,然后添加另一个重写和刷新规则.You could hook post save, check if the page has your archive template under the meta key `_wp_page_template`, then add another rewrite and flush rules.
- 1
- 2016-12-17
- Milo
-
- 2017-01-13
我已经通过
query_posts()
修改了主循环查询.为什么分页不起作用,如何解决?很好的答案创建的芯片现在需要修改.
一段时间以来,在执行主查询后,我们就有了$wp_the_query
变量,该变量应该等于$wp_query
全局变量.这就是为什么此部分来自芯片的答案:
没有主查询对象
不再需要. 我们可以通过创建临时变量来忘记这一部分.
// Pagination fix $temp_query = $wp_query; $wp_query = NULL; $wp_query = $custom_query;
现在我们可以打电话给
$wp_query = $wp_the_query;
甚至更好,我们可以致电:
wp_reset_query();
其他所有Chip概述的住宿项目. 在查询重置部分之后,您可以调用
f($wp_query)
的分页函数,它们依赖于全局$wp_query
.
为了进一步改善分页机制并赋予
query_posts
函数更多的自由,我创建了以下可能的改进:I have modified the main loop query via
query_posts()
. Why isn't pagination working, and how do I fix it?Great answer Chip created needs to be modified today.
For some time we have$wp_the_query
variable that should be equal to the$wp_query
global just after the main query executes.This is why this the part from the Chip's answer:
Hack the main query object
is not needed anymore. We can forget this part with creating the temporary variable.
// Pagination fix $temp_query = $wp_query; $wp_query = NULL; $wp_query = $custom_query;
So now we can call:
$wp_query = $wp_the_query;
or even better we can call:
wp_reset_query();
Everything other Chip outlined stays. After that query-reset-part you can call the pagination functions that are
f($wp_query)
, — they depend on$wp_query
global.
In order to further improve the pagination mechanics and to give more freedom to the
query_posts
function I created this possible improvement: -
- 2018-03-06
global $wp_query; $paged = get_query_var('paged', 1); $args = array( 'post_type' => '{your_post_type_name}', 'meta_query' => array('{add your meta query argument if need}'), 'orderby' => 'modified', 'order' => 'DESC', 'posts_per_page' => 20, 'paged' => $paged ); $query = new WP_Query($args); if($query->have_posts()): while ($query->have_posts()) : $query->the_post(); //add your code here endwhile; wp_reset_query(); //manage pagination based on custom Query. $GLOBALS['wp_query']->max_num_pages = $query->max_num_pages; the_posts_pagination(array( 'mid_size' => 1, 'prev_text' => __('Previous page', 'patelextensions'), 'next_text' => __('Next page', 'patelextensions'), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'patelextensions') . ' </span>', )); else: ?> <div class="container text-center"><?php echo _d('Result not found','30'); ?></div> <?php endif; ?>
global $wp_query; $paged = get_query_var('paged', 1); $args = array( 'post_type' => '{your_post_type_name}', 'meta_query' => array('{add your meta query argument if need}'), 'orderby' => 'modified', 'order' => 'DESC', 'posts_per_page' => 20, 'paged' => $paged ); $query = new WP_Query($args); if($query->have_posts()): while ($query->have_posts()) : $query->the_post(); //add your code here endwhile; wp_reset_query(); //manage pagination based on custom Query. $GLOBALS['wp_query']->max_num_pages = $query->max_num_pages; the_posts_pagination(array( 'mid_size' => 1, 'prev_text' => __('Previous page', 'patelextensions'), 'next_text' => __('Next page', 'patelextensions'), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'patelextensions') . ' </span>', )); else: ?> <div class="container text-center"><?php echo _d('Result not found','30'); ?></div> <?php endif; ?>
我已将自定义/辅助查询添加到模板文件/自定义页面模板;如何使WordPress使用我的自定义查询进行分页,而不是使用主查询循环的分页?
附录
我已经通过
query_posts()
修改了主循环查询.为什么分页不起作用,该如何解决?