分页显示基于日期的 archive.php 循环的空页 [英] Pagination shows empty pages for date based archive.php loop
问题描述
我正在使用 wordpress,并且有一个自定义帖子类型的自定义存档页面.自定义循环获取登录用户的注册日期,并且只显示在他们注册时或之后发布的帖子.效果很好.
I'm using wordpress and have a custom archive page for a custom post type. The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.
然而,分页仍然使用主查询,所以如果总共有 4 个帖子设置为每页 2 个帖子,即使由于登录用户注册日期只显示一个帖子,分页也总是显示有两页.
However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.
谁能帮我修改我拥有的内容,以便分页只显示超过 2 个帖子的结果?我已经尝试了几个小时,使用我在网上找到的各种更改...
Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
推荐答案
使用自定义存档和分页
@Scott Eldo 您创建自定义查询的方法不会更改自定义存档的主要查询.仅供参考,您是对的,分页仅适用于主查询.
Working with Custom Archive and Pagination
@Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.
在你的情况下,推荐的方法,我将使用过滤器 pre_get_posts
使用自定义存档和分页.请在此处查看我的回答如何修改帖子类型存档页面上的主要查询,您可以通过您的查询参数.
In your case, the recommended approach, I will use filter pre_get_posts
to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.
但是,如果您打算直接在模板中创建查询(即使它不更改主查询),您也需要将自定义查询与用于分页的 $GLOBALS['wp_query']
匹配并且不要忘记使用 wp_reset_query()
(必须).看看我在这里与您的代码相关的方法:
BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query']
that use in pagination and don't forget to use wp_reset_query()
( MUST ). Take a look my approach here related with your code:
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<?php
/**
* Fix Pagination in custom page/archive
* Set global wp_query the same as our custom query
*
* Use function the_posts_pagination( $args ); for pagination will work too
* see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
*/
$GLOBALS['wp_query'] = $my_query;
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data
另一种仍然不推荐的方法是使用 query_post
您的自定义查询.您可以在此处了解更多相关信息.
Another approach and still NOT recommended is use query_post
for your custom query. More reading about it you can learn more in here.
这篇关于分页显示基于日期的 archive.php 循环的空页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!