分页 WordPress 主循环

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

Paginating WordPress main loop

问题

  1. 我正在使用一个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),则存档页面上不会显示分页。

因此我的问题是:

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

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

&lt;?php
if ( have_posts() ) : 

    while ( have_posts() ) : 
        the_post(); 
        get_template_part( &#39;content&#39;, &#39;archive&#39; );
    endwhile; 

else :
    get_template_part( &#39;content&#39;, &#39;none&#39; ); 

endif;
?&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文件中进行主循环分页:

<?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:

&lt;?php if (have_posts()):
    while (have_posts()):
        the_post();

        get_template_part(&quot;content&quot;, &quot;archive&quot;);
    endwhile;

    // Add pagination links
    the_posts_pagination([
        &quot;mid_size&quot; =&gt; 2,
        &quot;prev_text&quot; =&gt; __(&quot;Previous&quot;, &quot;textdomain&quot;),
        &quot;next_text&quot; =&gt; __(&quot;Next&quot;, &quot;textdomain&quot;),
    ]);
else:
    get_template_part(&quot;content&quot;, &quot;none&quot;);
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:

确定