Home » Php » My pagination doesn't work correctly and won't show all existence posts in WordPress

My pagination doesn't work correctly and won't show all existence posts in WordPress

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’ve pagination for news on my website its seems to work correctly, but It won’t show all existence posts for each category. for example if I’ve got 25 posts in “Events” category and needed to have 5 pages (5 posts per page), but I’d have only 3 pages.

Here’s my code:

  <?php

         $ourCurrentPage = get_query_var('paged');

         $news = get_posts(array(
             'numberposts' => -1,
             'posts_per_page' => 5,
             'post_type' => 'news',
             'paged' => $ourCurrentPage,
             'tax_query' => array(
                    array(
                        'taxonomy' => 'news_category',
                        'field' => 'id',
                        'terms' => get_queried_object()->term_id,
                    )
                )
         ));
     ?>
    <!-- <?php $termm = urldecode(get_query_var( 'term' )) ?> -->
    <?php $termmm =  get_term_by( 'slug' , $termm, 'news_category' ); ?> 



    <section class="l-main">
        <section class="news-whole-bg is-full-height">

                <h1 class="news-main-title">
                    <?php echo single_term_title(); ?>
                </h1>

    <div class="container">
                <section class="news-wrapper clearfix">
                <?php foreach( $news as $newsItem ) :?>
                <?php $test = get_field('short_description', $newsItem->ID); ?>

      <div class="loaddd">
                  <div class="container">
                      <section class="m-news-part icon-link <?php echo ( $test == '' ) ? 'animate-off' : '' ?>">


                         <figure class="st-pusher k12" style="background-image:url(<?php the_field( 'main_image', $newsItem->ID ) ?>); background-size:cover;" ></figure>

                         <h3>
                            <p><?php echo get_the_date( null, $newsItem->ID ); ?></p>
                            <i><?php echo get_the_title( $newsItem->ID ) ?></i>
                            <span><?php the_field( 'tag_name', $newsItem->ID ) ?></span>
                         </h3>




                         <div class="st-menu">
                             <p>

                               <?php the_field( 'short_description', $newsItem->ID ) ?>

                             </p>
                         </div>

                        <a href="<?php echo qtrans_convertURL( get_permalink( $newsItem->ID ), qtrans_getLanguage() ) ?>">View more</a>


                      </section>
                  </div>


                <?php endforeach ?>

            </section>
    </div>

...
                    <div class="pagination_k">
                    <?php 

                        next_posts_link('Next Page', $news->max_num_pages);

                     ?> 
                    </div>
    </div>
        </section>
    </section>

for example check this category on my website it has 63 articles but there is only 7 pages. for 5 posts per each page we should have 13 pages.

URL

Answers: