所以我一直在调查为什么分页不起作用,但我什么也找不到。当它进入第2页时,它只显示与第一页相同的内容。还有其他想法吗?
1 ) {
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of "format" depends on whether we're using pretty permalinks
if( get_option('permalink_structure') ) {
$format = '?paged=%#%';
}
}
?>
'post',
'order' => 'DESC',
'posts_per_page' => 3,
'paged' => $paged
);
$blog_posts = new WP_Query($blogs); ?>
have_posts() ) : while ( $blog_posts->have_posts() ) : $blog_posts->the_post(); ?>
?>
Read The Post
str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $blog_posts->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( ' %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s ', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>更多信息:它目前被设置为index.php页面,尽管它不是主页。如果您通过错误拼写url找到它,它就会工作。
发布于 2018-10-10 00:12:28
你好,欢迎光临!做好阅读准备。
由于您正在处理index.php文件,所以我们需要考虑在Removing query_posts下的docs中主循环在分页Codex (链接)中说了什么:
例如,假设您的主题在主页(index.php)和类别页(category.php)上查询主循环,并且分页不起作用.
您需要做的是从WP_Query()文件中删除index.php并在function.php中执行一个操作。它在链接中被描述为一拍一拍,但我也会粘贴在这里:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_home()){
$query->set('posts_per_page', 3);
}
if(is_category()){
$query->set('posts_per_page', 3);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );之后,我们需要返回到index.php文件。在这一点上,应该是这样的:
while ( have_posts() ) :
the_post();
...
endwhile;
global $wp_query;
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var('paged') ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( ' %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s ', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );我进一步研究了您的代码,并建议阅读WordPress关于(到达_查询_瓦尔())的文档:
要在静态首页(页面模板)上获取当前分页编号,必须使用“page”查询变量.
经过进一步的研究,我在另一个与WP相关的问题上找到了这个答案(链接),内容如下:
这直接来自docs (链接):
如果分页在静态首页上中断,则必须以这种方式添加“分页”参数.
所以你在这里做的很棒:
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}
elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}
else {
$paged = 1;
}当我在带有自定义模板的static page上使用它时,您的代码工作得很好,直到我创建了它的主页,然后您需要在paginate_links()调用中为current参数使用$paged变量,如下所示:
paginate_links( array(
...
'current' => max( 1, $paged ),
...
) );我仍然不能在评论中提出问题,所以我希望我能有所帮助。祝好运!
https://wordpress.stackexchange.com/questions/316159
复制相似问题