Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress loop show limit posts

Tags:

wordpress

This is the basic loop

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

like image 456
ray Avatar asked Oct 06 '10 18:10

ray


Video Answer


3 Answers

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

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

  <?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; ?>
like image 130
Jiert Avatar answered Nov 09 '22 09:11

Jiert


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 ...
   }
 }
like image 23
nayeri Avatar answered Nov 09 '22 11:11

nayeri


You can limit the number of posts per loop through a $wp_query object. it takes multiple 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 on wp_query object here->

like image 29
nodefault Avatar answered Nov 09 '22 11:11

nodefault