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 been using Query Post for a newsticker which I have created so that the user navigate through the number of news by simply pressing Next & Previous buttons. But in case if there is only 1 news (i.e, only 1 post returned then there is no point of having Prev and Next button).

I will simply assign css attribute Display: none to the prev and next button if there is only 1 post. But how to find out if the query post returned just 1 post.

query_posts('post_type=post&cat=20&post_status=publish&order=DESC');     
while (have_posts()): the_post();
share|improve this question
    
I strongly recommend you look at: slideshare.net/andrewnacin/… –  Tom J Nowell Aug 7 '12 at 10:12

1 Answer 1

You should not use query_post when you can use the mutch faster query WP_Query.

You can count the post with the property post_count.

Here is a snippet that counts your post in the loop:

<?php

$args = array(
    'post_type'     =>  'post',
    'cat'           =>  '20',
    'post_status'   =>  'publish',
    'order'         =>  'DESC'

    );

// The Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

    if( ( $the_query->post_count <= 1 ) {
        echo 'One or less';

    } else {

        echo 'More than one post';
    }

endwhile;

// Reset Post Data
wp_reset_postdata();

?>
share|improve this answer
    
Something to read. –  kaiser Aug 7 '12 at 13:17

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.