Wordpress loop show limit posts

this is the main loop

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

I want to show 20 messages on the search results page, I know that we can change the value of the admin panel settings. but it will change everything, for example, the index page and archive pages, etc. I need to have them differently.

thanks!

+6
wordpress
source share
6 answers

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

Before calling the while statement, you need to request messages. So:

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

EDIT: Sorry for pagination, try the following:

  <?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; ?> 
+8
source share

If you do not want to compile a bunch of template files with different loops for different pages and keep pagination, try http://wordpress.org/extend/plugins/custom-post-limits/

+1
source share

Responses to a new request within the template will not work correctly with custom message types.

But the documentation suggests intercepting any request, checking its main request and changing it before execution. This can be done inside the template functions:

 function my_post_queries( $query ) { // do not alter the query on wp-admin pages and only alter it if it 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' ); 
+1
source share

You can limit the number of messages per loop through the $ wp_query object. It takes several parameters, for example:

 <?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 about the wp_query object here- >

0
source share

Add "paged" => $ paged pagination will work!

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

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 ... } } 
0
source share

All Articles