分页 WordPress 主循环

huangapple go评论93阅读模式
英文:

Paginating WordPress main loop

问题

  1. 我正在使用一个WP主题,在archive.php主循环中使用了以下代码:
  1. <?php
  2. if ( have_posts() ) :
  3. while ( have_posts() ) :
  4. the_post();
  5. get_template_part( 'content', 'archive' );
  6. endwhile;
  7. else :
  8. get_template_part( 'content', 'none' );
  9. endif;
  10. ?>

在某些分类中,我有很多文章,因此需要分页。如果我在WP设置中定义显示的最大文章数(例如20),则存档页面上不会显示分页。

因此我的问题是:

  1. 无论如何我可以对主循环进行分页吗,如果可以,如何操作?
  2. 还是我应该使用可以分页的WP查询?(我尝试过,但它不再显示正确的文章。)如果是这样的话,那个查询应该是什么样的?
英文:

I am using a WP theme that uses in the archive.php main loop. The code is like that:

  1. &lt;?php
  2. if ( have_posts() ) :
  3. while ( have_posts() ) :
  4. the_post();
  5. get_template_part( &#39;content&#39;, &#39;archive&#39; );
  6. endwhile;
  7. else :
  8. get_template_part( &#39;content&#39;, &#39;none&#39; );
  9. endif;
  10. ?&gt;

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:

  1. Can I paginate the main loop anyhow and if yes, how?
  2. 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文件中进行主循环分页:

  1. <?php if (have_posts()):
  2. while (have_posts()):
  3. the_post();
  4. get_template_part("content", "archive");
  5. endwhile;
  6. // 添加分页链接
  7. the_posts_pagination([
  8. "mid_size" => 2,
  9. "prev_text" => __("上一页", "textdomain"),
  10. "next_text" => __("下一页", "textdomain"),
  11. ]);
  12. else:
  13. get_template_part("content", "none");
  14. endif; ?>

希望这对您有所帮助。

英文:

Here is how to paginate the main loops (on archive.php) file:

  1. &lt;?php if (have_posts()):
  2. while (have_posts()):
  3. the_post();
  4. get_template_part(&quot;content&quot;, &quot;archive&quot;);
  5. endwhile;
  6. // Add pagination links
  7. the_posts_pagination([
  8. &quot;mid_size&quot; =&gt; 2,
  9. &quot;prev_text&quot; =&gt; __(&quot;Previous&quot;, &quot;textdomain&quot;),
  10. &quot;next_text&quot; =&gt; __(&quot;Next&quot;, &quot;textdomain&quot;),
  11. ]);
  12. else:
  13. get_template_part(&quot;content&quot;, &quot;none&quot;);
  14. endif; ?&gt;

huangapple
  • 本文由 发表于 2023年2月19日 23:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定