英文:
Paginating WordPress main loop
问题
- 我正在使用一个WP主题,在archive.php主循环中使用了以下代码:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'content', 'archive' );
endwhile;
else :
get_template_part( 'content', 'none' );
endif;
?>
在某些分类中,我有很多文章,因此需要分页。如果我在WP设置中定义显示的最大文章数(例如20),则存档页面上不会显示分页。
因此我的问题是:
- 无论如何我可以对主循环进行分页吗,如果可以,如何操作?
- 还是我应该使用可以分页的WP查询?(我尝试过,但它不再显示正确的文章。)如果是这样的话,那个查询应该是什么样的?
英文:
I am using a WP theme that uses in the archive.php main loop. The code is like that:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'content', 'archive' );
endwhile;
else :
get_template_part( 'content', 'none' );
endif;
?>
In some categories I have lots of articles, therefore pagination is necessary. If I define in the WP settings the max number of posts that are shown (e.g. 20) then no pagination is visible on the archive page.
Therefore my questions are:
- Can I paginate the main loop anyhow and if yes, how?
- Or should I use a WP query that can be paginated? (I tried it, but it did not show the right articles anymore.) If so: how that query might look like?
答案1
得分: 1
以下是如何在archive.php文件中进行主循环分页:
<?php if (have_posts()):
while (have_posts()):
the_post();
get_template_part("content", "archive");
endwhile;
// 添加分页链接
the_posts_pagination([
"mid_size" => 2,
"prev_text" => __("上一页", "textdomain"),
"next_text" => __("下一页", "textdomain"),
]);
else:
get_template_part("content", "none");
endif; ?>
希望这对您有所帮助。
英文:
Here is how to paginate the main loops (on archive.php) file:
<?php if (have_posts()):
while (have_posts()):
the_post();
get_template_part("content", "archive");
endwhile;
// Add pagination links
the_posts_pagination([
"mid_size" => 2,
"prev_text" => __("Previous", "textdomain"),
"next_text" => __("Next", "textdomain"),
]);
else:
get_template_part("content", "none");
endif; ?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论