0

I have a pagination issue in my custom loop, here is the loop.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args=array(
 'post_type' => 'post',
 'post_status' => 'publish',
 'paged'=>$paged,
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
if($i % 7 == 0) { ?> 
<?php
}
?>
    !!!LOOP CONTENT!!!

    <?php $i++; 
    if($i != 0 && $i % 7 == 0) { ?>
    <div class="clearfix"></div>
    <?php
    } ?>
    <?php  
    endwhile;
    }
    wp_reset_query();
    ?>
    <?php wpbeginner_numeric_posts_nav(); ?>

And my question is: Why pagination link not even showing up in this case?

  • 1
    What is wpbeginner_numeric_posts_nav()? It doesn’t even take the custom query or any parts of it as an argument, so I don’t know how it’s even supposed to work. – Jacob Peattie May 20 '18 at 11:36
  • My bad, the rest of it is in the function.php file. But normally I tried to use this: <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div> <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div> but it doesnt work anyway – Isuusu May 20 '18 at 14:39
  • for using next_posts_link() with a custom query, read codex.wordpress.org/Function_Reference/… – Michael May 20 '18 at 18:36
  • Ok, it's done with net_posts_link() but how can I change this to wp_pagenavi with numerical pagination? – Isuusu May 20 '18 at 21:41
0

Step:1 Put step 1 code in function.php

// numbered pagination
function pagination($pages = '', $range = 4)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
         echo "</div>\n";
     }
}
?>

Step:2

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$args = array(
   'posts_per_page' => 6,
   'paged' => $paged
);

Step: 3 After while loop completed before wp_reset_query();

<?php if (function_exists("pagination")) {
    pagination($my_query->max_num_pages);
} ?>

NOTE: Reference link : after while change wp_reset_query(); TO wp_reset_postdata();

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.