Get the number of posts shown in the current page


I am currently trying to implement a load more function described in this stackoverflow post, and in order to do so it is necessary to know how many post have been loaded already. Thus, I am trying to return the number of current posts in a given page.

The code in question is the following:

 <div class="row">
            $posts = new WP_Query(array(
                'offset' => 0,
                'posts_per_page' => 6
            while ($posts->have_posts()) {
                $posts->the_post(); ?>

                <div class="card__wrapper col-sm-12 col-md-6 col-lg-4 ">
                    <?php get_template_part('partials/card', 'page'); ?>
            <?php }
            wp_reset_postdata(); ?>

        global $wp_query;
        // if ($wp_query->max_num_pages > 1)
            echo '<div class="row"><div class="col-sm-12"><div class="misha_loadmore">More posts</div></div></div>'; // you can use <a> as well

Even though, the WP_Query returns only 6 posts, the functions get_query_var('posts_per_page') and $wp_query->post_count, when called, return 10. Any ideas, what I am missing here?

Thanks in advance.

, vtolentino 4 years 2020-03-29T04:50:55-05:00 0 Answers 104 views 0

Leave an answer