Link to home
Start Free TrialLog in
Avatar of Hari Shankar
Hari ShankarFlag for India

asked on

Wordpress Pagination Function Not working ?

Hi Experts,

Am trying to create a custom page in wordpress that will show the post . But when i give the pagination previous_posts_link() ; and next_posts_link( ); it doesn't work . Please see the below code

<?php if (have_posts()) :?>

<?php $custom_query = new WP_Query('cat=2'); // exclude category 9
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

          <div class="col-md-12 post-holder">
            <div class="well-box">
               <div class="post-image"><a href="#"><img src="<?php the_post_thumbnail( '', array('class' => 'img-responsive'));?>"</a></div>
<h1 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>


 <div class="post-meta"> <span class="date-meta">ON <a href="#"><?php the_date(); ?></a> /</span> <span class="admin-meta">BY <a href="#"><?php the_author(); ?></a> /</span> <span class="tag-meta">IN <a href="#"><?php the_tags(); ?></a> /</span> <span class="comment-meta"><?php comments_number('No','1','%'); ?> <a href="#">Comments</a></span> </div>

   <p> <?php the_excerpt(); ?></p>
  </div>
</div>
<!-- /.blog holder -->
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

         
          
          
          <!-- /.blog holder -->
          <div class="col-md-12 tp-pagination">
            <ul class="pagination">
              <li> <a href="#" aria-label="Previous"> <span aria-hidden="true"><?php previous_posts_link(); ?></span> </a> </li>
              <li class="active"><a href="#">1</a></li>
              <li><a href="#">2</a></li>
              <li><a href="#">3</a></li>
              <li><a href="#">4</a></li>
              <li><a href="#">5</a></li>
              <li>
<?php next_posts_link( ); ?>


    <?php endif; ?>

Open in new window

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Looks like you're missing the "paged" query parameter. Try altering line 3 (as per your code above) as follows:
<?php 
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query('cat=2&paged=' . $paged'); // exclude category 9

Open in new window

Documentation: https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
Avatar of Hari Shankar

ASKER

Hi Terry Woods thanks for the info . Let me try
Also, don't forget to include: 'posts_per_page=3'
3 being the number of posts per page you would like to see.
It seems not working . I tried with the


 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
   $custom_query = new WP_Query('cat=6&posts_per_page=6&paged='.$paged); 
  $pagination = array(
            'base' => '%_%',
            'format' => '?page=%#%',
            'total' => $the_query->max_num_pages,
            'prev_next' => True,
            'prev_text' => __( '<< Previous' ),
            'next_text' => __( 'Next >>' )
        );


echo paginate_links($pagination);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carlos Llanos
Carlos Llanos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried with your code . But Still am not getting the pagination for my Blog page