Knowing the total number of posts before to get into the loop

functions.php:

function wpse8170_get_posts_count() {
    global $wp_query;
    return $wp_query->post_count;
}

index.php:

if (have_posts()) :
    echo '<h1>' . wpse8170_get_posts_count() . ' Posts Found</h1>';

    while ( have_posts() ) : 
        the_post();
        //...
    endwhile;
endif;

Leave a Comment