Wordpress 循环显示限制帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3875895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:39:25  来源:igfitidea点击:

Wordpress loop show limit posts

wordpress

提问by ray

This is the basic loop

这是基本循环

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

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

I want to show 20 posts on the search results page. I know we can change the value on admin panel options but it will change all i.e. index page and archive page etc. I need to have them differently.

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

回答by Jiert

Great reference: http://codex.wordpress.org/The_Loop

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

Just before you call the while statement, you need to query the posts. So:

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

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

  <?php while (have_posts()) : the_post(); ?>
    <!-- Do stuff... -->
  <?php endwhile;?>

EDIT: Sorry about the pagination, try this:

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

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

回答by nayeri

I find this solution and it works for me.

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

 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 ...
   }
 }

回答by Николай Конев

Answers with new query inside template will not work correctly with custom post types.

在模板中使用新查询的答案将无法与自定义帖子类型一起正常工作。

But documentationoffers to hook on any query, check is it main query, and modify it before execution. That can be done inside template functions:

但是文档提供挂钩任何查询,检查它是主查询,并在执行前修改它。这可以在模板函数中完成:

function my_post_queries( $query ) {
  // do not alter the query on wp-admin pages and only alter it if it's the main query
  if (!is_admin() && $query->is_main_query()) {
    // alter the query for the home and category pages 
    if(is_home()){
      $query->set('posts_per_page', 3);
    }

    if(is_category()){
      $query->set('posts_per_page', 3);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

回答by n1cko

You can limit the number of posts per loop through a $wp_query object. it takes multiple parameters for example:

您可以通过 $wp_query 对象限制每个循环的帖子数。它需要多个参数,例如:

<?php 
$args = array('posts_per_page' => 2, 'post_type' => 'type of post goes here');
$query = new WP_Query( $args );
while( $query->have_posts()) : $query->the_post();
<!-- DO stuff here-->
?>

More on wp_query object here->

更多关于 wp_query 对象在这里->

回答by Daman

Add 'paged' => $paged Pagination would work!

添加 'paged' => $paged 分页会起作用!

<?php 
$args = array('posts_per_page' => 2, 'paged' => $paged);
$query = new WP_Query( $args );
while( $query->have_posts()) : $query->the_post();
<!-- DO stuff here-->
?>