how to change the number of posts returned in a specific loop without making it global?

the website i’m referring to is technibility.com. If you look at it the slider on top is using this piece of code

<?php if(is_home()) { ?>
<div id="ca-container" class="ca-container">
<div class="ca-wrapper">

<?php while (have_posts()): the_post(); ?>
<div class="ca-item ca-item-2">
<div class="ca-item-main">

<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>">
 <?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
               
<h3><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h3>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php } ?>

problem is that if i used

Read More
query_posts('showposts=20');

its going to affect the entire site and show me 20 articles in the carousel and the posts on the page. How can i force it to only take affect with the slider?

also is there a way to have the_post_thumbnail() not stretch the image and use 150px height and any width but keeping it under 300px ?

Related posts

1 comment

  1. You need to pass an array to WP_Query like this

    then after you’ve ended the query use this to reset the query data.

    <?php wp_reset_query(); ?>
    

    example of standard query using WP_Query

    $arg = array(
    'posts_per_page' => 20
    );
    
    $query = new WP_Query($args)
    //start to loop your things here
     <?php if ($query->have_posts()) : ?>
     <?php while ($query->have_posts()) : $query->the_post(); ?>
    // your custom code here
    endif; endwhile;
    

Comments are closed.