Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
1  
Hi Rarst, thank you for your help. It makes sense - so by adding the ampersandis how I can add more conditions. I also now have a better understanding of the array key value pair. I've added your wp_reset_postdata() and will do so going forward. Awesome! –  brett Apr 4 '11 at 9:32
add comment

I think you have a slight error here, the 'cat' paramater only accepts a post ID as an Integer (Source: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters);

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.