【问题标题】:How To Make Pagination For Custom Post Type In Wordpress?如何在 Wordpress 中为自定义帖子类型进行分页?
【发布时间】:2021-07-31 20:31:34
【问题描述】:
前提条件:
- 我创建了帖子类型“比较”。
- 我仅为“比较”帖子类型创建了存档页面。
任务:我需要在“比较”的存档页面创建分页。
问题:我尝试使用
<?php echo paginate_links(); ?>
但它不起作用,请帮助。
【问题讨论】:
标签:
php
wordpress
pagination
custom-post-type
【解决方案1】:
试试下面的代码
$query = new WP_Query(
array(
'posts_per_page'=>30,
'post_type'=>'comparison',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
//your code
endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
}
wp_reset_postdata();