0

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();
1

1 Answer 1

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();

?>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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