我有一个自定义的文章类型在WordPress和存档页面上,我想添加分页。我有以下代码,但当我点击第2页或“下一页”,它重新加载第1页。
我的第一个想法是它应该被添加到wp_reset_postdata()之前,但是这会在每个帖子旁边显示链接。
我错过了什么?
<!-- custom query for village life posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 6,
'post_type' => 'villagelife',
'orderby' => 'post_date',
'order' => 'ASC',
'paged' => $paged,
);
$villagelifeArchive = new WP_Query($args);
while($villagelifeArchive->have_posts()) {
$villagelifeArchive->the_post();
?>
<div class="grid-item">
<div class="grid-item-wrapper">
<div class="grid-item-container">
<?php
if ( has_post_thumbnail() ) :
?>
<div class="grid-image-top">
<span class="centered project-image-bg"><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('archive-village-life-image'); ?></a>
</span>
</div>
<?php
endif;
?>
<div class="grid-item-content">
<span class="item-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
<span class="item-category"><?php echo get_the_term_list($post->ID, 'village-life-category', '', ', ', ' '); ?></span>
<span class="item-excerpt"><?php echo wp_trim_words(get_the_content(), 18);?></span>
<?php echo '<a class="button ast-button more-info" href='.get_the_permalink() . ">Read More..</a>"; ?>
</div>
</div>
</div>
</div>
<?php
} wp_reset_postdata();
$total_pages = $villagelifeArchive->max_num_pages;
print_r($total_pages); //give me 2 ..the correct answer.
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
?> <div> <?php
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
));
?> </div> <?php
}
?>
1条答案
按热度按时间fcg9iug31#
我目前无法帮助您的问题,但我已经得到了我用于分页的php。
将以下内容添加到functions.php文件中