【问题标题】:controlling the number of posts on a wordpress archive page控制 wordpress 存档页面上的帖子数量
【发布时间】:2010-09-08 14:47:50
【问题描述】:

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

我该怎么做?

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

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

【问题讨论】:

    标签: php wordpress archive


    【解决方案1】:

    只需在查询后附加标签参数(如下所示: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 应该包含所有默认参数,例如当前标签、类别、年份或您正在查看的任何存档页面。

    【讨论】:

    • 嗯,好的。获取存档页面标签的最佳方法是什么?因为我不想使用 get 参数。
    • 查看我的编辑以获取一种可能的解决方案。这是你需要的吗?
    【解决方案2】:

    你可以试试这个:

      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

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

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2016-06-05
      • 1970-01-01
      相关资源
      最近更新 更多