0

I want to add number pagination to my blog page. I have tried to add pagination code but it does not appear. here is the code and I want to add numbers pagination to it. it is blog page

<?php
               $args = array(
                    'post_type' => 'post'
                );
            
                $post_query = new WP_Query($args);

            if($post_query->have_posts() ) {
                while($post_query->have_posts() ) {
                    $post_query->the_post();
                    ?>

         <div class="cbp-item">
            <div class="news_item shadow">
               <a class="image" href="<?php the_permalink();?>">
                  <img src="<?php the_post_thumbnail_url(); ?>" alt="Latest News" class="img-responsive">
               </a>
               <div class="news_desc">
                  <h3 class="text-capitalize font-light darkcolor"><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
                  <ul class="meta-tags top20 bottom20">
                     <li><a href="#."><i class="fa fa-calendar"></i><?php echo get_the_date( 'M d' ); ?></a></li>
                     <li><a href="#."> <i class="fa fa-user" aria-hidden="true"></i> <?php the_author_posts_link();?> </a></li>
                     <li><a href="#."><i class="fa fa-comment-o"></i><?php comments_number( 'no responses', 'one response', '% responses' ); ?></a></li>
                  </ul>
                  <p class="bottom35"><?php the_excerpt(); ?></p>
                  <a href="<?php the_permalink();?>" class="button btnprimary btn-gradient-hvr">Read more</a>
               </div> 
            </div> 
         </div> 

         <?php
            }
        }
        
           ?>

0

First of all you need to get the paged query variable and pass it to your main query args:

//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
    'post_type' => 'post'
    'paged' => $paged,
);

Then you have to add the following code at the place where you want the pagination to appear:

<?php
    $big = 999999999; // need an unlikely integer
     
    echo paginate_links( array(
        'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format'    => '?paged=%#%',
        'current'   => max( 1, get_query_var('paged') ),
        'total'     => $post_query->max_num_pages
    ) );
?>
| improve this answer | |
  • thanks so much for your reply. please, where could I put the first code in the blog? – social Jul 14 at 13:35
  • Are you using a custom template for your blog? If yes then you can put it there. In general whenever you are making a WP_Query and you want to have pagination you are following the above process. Let me know if that worked :) – giannisrig Jul 16 at 6:55
  • ``` $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $args = array( 'post_type' => 'post' 'paged' => $paged, ); ``` where should I put this code in the code I provided above? or in functions.php? – social Jul 16 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.