控制 WordPress 存档页面上的帖子数量

发布于 2024-09-18 01:39:29 字数 244 浏览 4 评论 0原文

我已将我的 WordPress 博客的首页设置为仅显示三篇文章。 在 archive.php 模板上,当查看某个标签的帖子时,我想显示 10 个结果。

我该怎么做?

我尝试了这个 php 代码。但它不是只显示带有特定标签的帖子,而是查询所有最近的帖子。

//in archive.php (before the loop)
query_posts('posts_per_page=10');

I have set my wordpress blog's frontpage to show only three posts.
On the archive.php template, when the posts of a tag are viewed, I want to show 10 results.

How do I do this?

I tried this php code. But instead of only showing the posts with a certain tag, it queries all recent posts.

//in archive.php (before the loop)
query_posts('posts_per_page=10');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-09-25 01:39:38

你可以试试这个:

  function post_per_page_control( $query ) {
     if ( is_admin() || ! $query->is_main_query() )
       return;

    // For archive.You can omit this
     if ( is_archive() ) {
          //control the numbers of post displayed/listed (eg here 10)
          $query->set( 'posts_per_page', 10 );
          return;
     }

     // For your tag
     if ( is_tag() ) {
          //control the numbers of post displayed/listed (eg here 10)
          $query->set( 'posts_per_page', 10 );
          return;
     }
  }
  add_action( 'pre_get_posts', 'post_per_page_control' );

在这里阅读更多内容:

1 http://codex.wordpress.org/Plugin_API/Action_Reference /pre_get_posts

2 http://codex.wordpress.org/Function_Reference/query_posts

can you try this:

  function post_per_page_control( $query ) {
     if ( is_admin() || ! $query->is_main_query() )
       return;

    // For archive.You can omit this
     if ( is_archive() ) {
          //control the numbers of post displayed/listed (eg here 10)
          $query->set( 'posts_per_page', 10 );
          return;
     }

     // For your tag
     if ( is_tag() ) {
          //control the numbers of post displayed/listed (eg here 10)
          $query->set( 'posts_per_page', 10 );
          return;
     }
  }
  add_action( 'pre_get_posts', 'post_per_page_control' );

read more here:

1 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

2 http://codex.wordpress.org/Function_Reference/query_posts

转瞬即逝 2024-09-25 01:39:36

只需在查询中附加标签参数即可(如下所示: http://codex.wordpress.org /Function_Reference/query_posts#Tag_Parameters):

query_posts('posts_per_page=10&tag=your_desired_tag');

编辑:如果您在函数中使用此功能,您也可以将限制附加到原始查询,如下所示:

function my_archive_loop($content) {
    global $query_string;
    query_posts($query_string . "&posts_per_page=10"); 
}

变量 $query_string< /code> 应该包含所有默认参数,例如当前标签、类别、年份或您正在查看的任何存档页面。

Just append the query with a tag parameter (as shown here: http://codex.wordpress.org/Function_Reference/query_posts#Tag_Parameters):

query_posts('posts_per_page=10&tag=your_desired_tag');

EDIT: If you use this within a function you can also just append your limit to the original query like this:

function my_archive_loop($content) {
    global $query_string;
    query_posts($query_string . "&posts_per_page=10"); 
}

The variable $query_string should than include all the default parameters like the current tag, category, year or whatever archive page you are viewing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文