1
votes

Archives page while loop is not working properly. The particular category has 5 posts published. But when I navigate to the archives page for that category it is only displaying 3 posts.

Tried to display number of posts of that particular category using

$count = $GLOBALS['wp_query']->found_posts;

It is returning the proper posts count (total 5 posts). But when continued to the while loop have_posts() it is displaying only 3.

Tried wp_reset_query after endwhile, but didn't work. Also tried with get_categories() function to display the number of counts in each category it returned correct count but displayed only 3 posts in the while loop.

Below is the archive.php code.

<div class="row">

<?php
while (have_posts()):
    the_post();
?>
    <div class="col-sm-12 col-md-8">
        <div class="blog-post small">
            <div class="post-header">
                <p class="categories"><a href="#"><?php the_category(', ') ?></a></p>

                <h2 class="post-title">
                    <a href="<? the_permalink(); ?>"><?php the_title(); ?></a>
                </h2>

                <ul class="post-meta">
                    <li class="date"><?php the_time('F jS, Y') ?></li>
                    <li class="author">by <a href="<?php the_permalink() ?>"><?php the_author_posts_link(); ?> </a></li>
                    <li class="comments"><a href="<?php the_permalink() ?>"><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></a></li>
                </ul>
            </div>

            <div class="post-cover">
<?php
    $post_id = get_the_ID();

    $meta_info = get_post_meta($post_id, '_thumbnail_id', true);
    $image = wp_get_attachment_image_src($meta_info, $size = 'full', $icon = false);
?>
                <a href="<?php the_permalink(); ?>">
    
                    <img width="367" height="346" src="<?php echo $image[0]; ?>" alt="simple post cover" />
                </a>
            </div>
    
            <div class="post-body">
                <p><?php the_excerpt(); ?></p>
    
                <div class="align-center">
                    <a href="<?php the_permalink() ?>" class="btn template-btn-2">Read more</a>
                </div>
            </div>
        </div>
    </div>

<?php
endwhile;
?>
</div>

What should I do? Any thoughts?

1
what is your "Posts per page" set to in the backend?Stender
@Stender Blog pages show at most 10 posts . same is enabled in wp admin.bhavya
when clicked on author's name , all the post of him will be listed properly in archives page . But this is not happening with the category.bhavya
That means you can set your query vars with either pre_get_post or similar - maybe something like set_query_var('post_per_page', 10); - I would create a filter, that checks your category and sets the var.Stender

1 Answers

0
votes

Try like this if you still need :-)

<?php query_posts('showposts=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>

    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <p>?php the_time(); ?></p>
    <?php the_content(); ?>
    <p><?php the_tags(); ?></p>

<?php endwhile;?>

    <p><?php next_posts_link(); ?></p>
    <p><?php previous_posts_link(); ?></p>

<?php else : ?>

    <h1>Not Found</h1>
    <p>Silly monkey.</p>

<?php endif; wp_reset_query(); ?>

If found this in someone blog but maybe who used this.