我在侧栏上有一个带日历的活动页面。每次用户点击日期时,我都会通过我创建的API端点加载该日期的所有事件。 到目前为止一切都很好,但我有一个分页问题。 如果用户刚刚进入事件页面分页将完美,但是当用户点击日历以通过API加载事件时,它会破坏分页并始终将next_post_link作为第2页返回。 只是因为我也在设置中配置了posts_per_page与自定义查询相同,但它没有帮助。
<?php
function get_events(WP_REST_Request $request)
{
global $post;
$event_date = $request["event_date"];
$paged = ($request["paged"]) ? $request["paged"] : 1;
$date = '';
$args = array(
'post_type' => 'event',
'posts_per_page' => 3,
'paged' => $paged,
'meta_key' => 'event_date',
'meta_value' => $event_date,
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
ob_start();
include(locate_template('events_template.php'));
$events = ob_get_contents();
ob_get_clean();
return $events;
}
活动模板:
<div class="posts-container">
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<?php
//Here we group events by event_date and add the date at the top.
$event_date = get_post_meta( $post->ID, 'event_date', true );
if ( $event_date != $date )
{
$event_date_formatted = date( 'l, F jS, Y', strtotime( $event_date ) );
echo "<p class='page-header'><strong>$event_date_formatted</strong></p>";
$date = $event_date;
}
?>
<div class="event ath-card">
<h4><?php the_title(); ?></h4>
<p><?php echo get_post_meta( $post->ID, 'event_location', true ) ?></p>
<p><?php echo get_post_meta( $post->ID, 'event_details', true ) ?></p>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<div class="loader text-center hidden">
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div class="div pagination-container text-center">
<?php
//always returns page number 2.
//I run the exact query in the page itself and it works fine
next_posts_link( '<button class="ath-btn ath-btn-info">LOAD MORE</button>', $the_query->max_num_pages );
?>
</div>
答案 0 :(得分:0)
好吧,我无法以正常方式解决问题,但幸运的是
<?php $the_query->max_num_pages ?>
返回了正确的总页数,因此我用它来手动设置下一个帖子链接。
我补充说:
if($paged < $the_query->max_num_pages)
{
$next_page = $paged + 1;
}
else
{
$next_page = null;
}
而且:
<?php if ( $next_page ) : ?>
<a href="http://localhost:8888/water/wp-json/events/v1/events/page/<?php echo $next_page ?>/?event_date=<?php echo $request["event_date"] ?> ">
<button class='ath-btn ath-btn-info'>LOAD MORE</button>
</a>
<?php endif; ?>