Wordpress循环显示限制帖子

ray*_*ray 7 wordpress

这是基本循环

<?php while (have_posts()) : the_post(); ?>

我想在搜索结果页面上显示20个帖子.我知道我们可以更改管理面板选项的值,但它会更改所有,即索引页面和存档页面等.我需要有不同的方式.

Jie*_*ert 10

很棒的参考:http: //codex.wordpress.org/The_Loop

在调用while语句之前,您需要查询帖子.所以:

  <?php query_posts('posts_per_page=20'); ?>

  <?php while (have_posts()) : the_post(); ?>
    <!-- Do stuff... -->
  <?php endwhile;?>
Run Code Online (Sandbox Code Playgroud)

编辑:对于分页抱歉,试试这个:

    <?php 
        global $query_string;
        query_posts ('posts_per_page=20');
        if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <!-- Do stuff -->
    <?php endwhile; ?>

    <!-- pagination links go here -->

    <? endif; ?>
Run Code Online (Sandbox Code Playgroud)


小智 7

我找到了这种解决方案,它对我有用。

 global $wp_query;
 $args = array_merge( $wp_query->query_vars, ['posts_per_page' => 20 ] );
 query_posts( $args );

 if(have_posts()){
   while(have_posts()) {
     the_post();
     //Your code here ...
   }
 }
Run Code Online (Sandbox Code Playgroud)