我在计算分页时遇到了一个问题。使用我在这里找到的信息Wordpress Pagination Not Working - Always Showing First Pages Content 线程,我已成功在页面上设置分页。代码如下所示:
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
\'posts_per_page\' => 10,
\'paged\' => $paged,
\'cat\' => 6,
);
$your_query = new WP_Query( $args );
if ( $your_query->have_posts() ) { ?>
<div id="blog-masonry" class="row">
<?php while ( $your_query->have_posts() ) {
$your_query->the_post();
get_template_part( \'template-parts/content\', get_post_format() );
}
} else {
echo \'Sorry, no posts found.\';
}
?>
问题是我访问这些页面的唯一方法是在浏览器中键入例如/page/2。如何实际显示分页(<;1 2 3>)?
抱歉,如果这个问题很愚蠢,我完全是个新手。
解决方案(按照Vishit的建议):在循环后添加分页
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url(
get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'prev_text\' => __(\'Prev\'),
\'next_text\' => __(\'Next\'),
\'total\' => $your_query->max_num_pages
最合适的回答,由SO网友:Vishit 整理而成
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args_blog = array(
\'post_type\' => \'post\',
\'paged\' => $paged,
\'posts_per_page\' => get_option(\'posts_per_page \')
);
$query_blog = new WP_Query( $args_blog );
if($query_blog->have_posts()) : while($query_blog->have_posts()) : $query_blog->the_post();
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'prev_text\' => __(\'Prev\'),
\'next_text\' => __(\'Next\'),
\'total\' => $query_blog->max_num_pages
) );