How to limit the posts

I have a new question, how can I limit the amount of posts coming out of this query? I only need 7

<?php
$newsposts = new WP_Query('cat=restaurant');
if ( is_front_page()) { 
    echo '<h3 class="member-review">Latest Restaurants</h3>
    <div id="extra">';
    if ($newsposts->have_posts()) : while ($newsposts->have_posts()) : $newsposts->the_post();
        echo '<div class="reslogo"><img src="'.catch_that_image().'"/></div>';
    endwhile; endif; 
    echo '</div>';    
} 
?>

I tried to put: ('cat=restaurants'.'limit=7') but she no work. How did i go wrong? any help would be appreciated

3 Answers
3

It should be:

$newsposts = new WP_Query('cat=restaurant&posts_per_page=7');

Another way to write it (helps readability with larger queries) would be:

$newsposts = new WP_Query(array(
    'cat' => 'restaurant',
    'posts_per_page' => 7,
));

See WP_Query in Codex for description of available parameters.

PS would be good practice to add wp_reset_postdata() at the end. You are (correctly) not modifying main query, but you do change global $post variable with this loop.

Leave a Comment