我正在使用自定义查询对此网站进行分页,并get_next_posts_link, get_previous_posts_link. 问题是链接到旧条目(get_next_posts_link) 只工作一次,这意味着如果我第二次单击它,它将始终指向主页,这很奇怪,因为当我检查链接时,href属性是:http://localhost:8888/athena/event/page/3
.
根据变量,共有7页$queryObject->max_num_pages
显示我的意思的小屏幕捕获视频(27秒长):https://www.useloom.com/share/f8f9ecac9dd54a49aa3613f9c0f5c9f9
这是我的代码:
<!-- section list events-->
<?php
if(get_query_var(\'paged\')){
$paged = get_query_var(\'paged\');
} elseif (get_query_var(\'page\')) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
$query_args = array(
\'post_type\' => \'event\',
\'posts_per_page\' => 3,
\'paged\' => $paged
);
$queryObject = new WP_Query($query_args);
?>
<section class="block-list-events">
<div class="container">
<div style="color: #000;">
<?php var_dump($queryObject->found_posts); ?>
</div>
<div class="list-events">
<?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
<div class="item clearfix">
<div class="img tbl pull-left">
<div class="tbl-cell date">
<p><?php the_time(\'Y M\') ?></p>
<p><span><?php the_time(\'j\') ?></span></p>
</div>
<div class="tbl-cell img-a">
<a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(\'event-single\'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
</div>
</div>
<div class="info pull-left">
<p class="tag"><?php the_field(\'label\'); ?></p>
<h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
<p class="desc"><?php echo excerpt(25); ?></p>
<div class="button-view-detail">
<a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<div class="clearfix">
<!-- Pagination -->
<?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( \'Older Entries\', $queryObject->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( \'Newer Entries\', $queryObject->max_num_pages ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
</section>
<!-- /end of section list events -->
</main>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
根据@amit的建议,我已经更新了代码,但结果仍然与之前一样:
<!-- section list events-->
<?php
if(get_query_var(\'paged\')){
$paged = get_query_var(\'paged\');
} elseif (get_query_var(\'page\')) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
$query_args = array(
\'post_type\' => \'event\',
\'numberposts\' => -1,
\'posts_per_page\' => 3,
\'paged\' => $paged
);
$queryObject = new WP_Query($query_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $queryObject;
?>
<section class="block-list-events">
<div class="container">
<div style="color: #000;">
<?php var_dump($queryObject->found_posts); ?>
</div>
<div class="list-events">
<?php if ($queryObject->have_posts()): while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
<div class="item clearfix">
<div class="img tbl pull-left">
<div class="tbl-cell date">
<p><?php the_time(\'Y M\') ?></p>
<p><span><?php the_time(\'j\') ?></span></p>
</div>
<div class="tbl-cell img-a">
<a href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(\'event-single\'); ?>" width="530" height="300" alt="<?php the_title(); ?>"/></a>
</div>
</div>
<div class="info pull-left">
<p class="tag"><?php the_field(\'label\'); ?></p>
<h4><a href="<?php echo get_the_permalink() ?>"><?php the_title(); ?></a></h4>
<p class="desc"><?php echo excerpt(25); ?></p>
<div class="button-view-detail">
<a class="btn btn-3" href="<?php echo get_the_permalink() ?>" title="<?php the_title(); ?>">View Details</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php
// Reset postdata
wp_reset_postdata();
?>
<div class="clearfix">
<!-- Pagination -->
<?php if ($queryObject->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( \'Older Entries\', $queryObject->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( \'Newer Entries\' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
</div>
</section>
<!-- /end of section list events -->
</main>
<?php
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
<?php get_footer(); ?>