Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress loop is not showing all posts

Tags:

php

wordpress

I got lots of articles I show on my category page.

Here is the code.

<?php if(is_category(4)) { 
    while ( have_posts() ) : the_post(); ?>
        <div class="work">
            <div class="work-thumb">
                <a href="<?php echo get_permalink(); ?>">
                    <?php the_post_thumbnail(); ?>
                </a>
            </div>
            <div class="work-title">
                <a href="<?php echo get_permalink(); ?>"> <?php the_title(); ?></a>
            </div>
        </div>
    <?php endwhile; // end of the loop.
} ?>

It is just loop on all articles, but it doesn't show all of them, just like 50%

What could be the problem>?

like image 317
user3649628 Avatar asked Sep 20 '26 18:09

user3649628


1 Answers

Is the page that's using that template/code the page that you have set to be the Posts Page, in admin settings?

If so, then the posts per page setting could be less than the total number of posts (and you would need pagination, or to increase this number).

If it's a custom query with the code in your question, then you need to add this to the query arguments:

'posts_per_page' => -1

Note: Even if your case is the former, you could alter the query by using the pre_get_posts filter. E.g. put this in your theme's functions.php:

add_action('pre_get_posts', 'my_filter');

function my_filter( $query ){
    $query->set('posts_per_page', -1);
    return $query;
}

Inside that function, you want to wrap the code inside an if statement, to do it specifically for the, for example, post type or taxonomy in question.

like image 120
Dan. Avatar answered Sep 22 '26 09:09

Dan.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!