14
<?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $loop = new WP_Query(
            array(
                'post_type' => 'html5-blank',
                'posts_per_page' => 5,
                'paged'=>$paged
            )
        );
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>      
 //Loop Code Here..
 <?php wp_reset_query(); ?> 
   <nav>
        <?php previous_posts_link( 'Newer posts &raquo;' ); ?>
        <?php next_posts_link('Older &raquo;') ?>
    </nav>
<?php endwhile; ?>
<?php else: ?>

Url on next page as I input result is: www.mywebsite.com/blog/page/2 is WORKING. But I can't show the pagination links.

Where did it go wrong?

EDIT: Pagination link is showing in page/2/ but not in the main blog page. Why?

5 Answers 5

24

There are 3 ways that I would suggest for pagination with a custom post wp_query. Unfortunately to this day there isn't a lot of good information about this out there, or at least what is out there is unclear in some cases. Hopefully this helps!

Note, you also did have the wp_reset_postdata() in the wrong place, but even still more is needed to get it to work correctly.

Option 1 - use max_num_pages variable

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 
        'posts_per_page' => 1, 
        'paged' => $paged, 
        'post_type' => 'cpt_type'
    );
    $cpt_query = new WP_Query($args);
?>

<?php if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) : $cpt_query->the_post(); ?>

    //Loop Code Here...

<?php endwhile; endif; ?>

<nav>
    <ul>
        <li><?php previous_posts_link( '&laquo; PREV', $cpt_query->max_num_pages) ?></li> 
        <li><?php next_posts_link( 'NEXT &raquo;', $cpt_query->max_num_pages) ?></li>
    </ul>
</nav>

You'll see above, a slightly different format for previous_posts_link and next_posts_link which now access the max_num_pages variable. Be sure to use your own query variable name when accessing max_num_pages. Notice I use $cpt_query since that is the variable for my query example.

Option 2 - temporarily use the $wp_query variable for your loop query

This is what a lot of folks recommend, but be careful to asign the $wp_query variable to a temp variable and re-assign it or you will run in to all kinds of troubles. Which is why I recommend Option #1. As noted on CSS Tricks, you can do something like this:

<?php 
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=6&post_type=news'.'&paged='.$paged); 

  while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

  <!-- LOOP: Usual Post Template Stuff Here-->

<?php endwhile; ?>

<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

Option 3 - use WP-pagenavi plugin

Just as another option what you can do instead is use the WP-pagenavi plugin, and setup your query as in Option #1. But make one change in the code, remove everything within the element and replace with this function, once you have installed the plugin. So you'll end with:

<nav>
    <?php wp_pagenavi( array( 'query' => $cpt_query ) ); ?>
</nav>
4

I think you put <?php wp_reset_query(); ?> in the wrong place.. shouldn't it be next or after pagination codes?

something like this

<?php endwhile; ?>
<?php else: ?>
<?php wp_reset_query(); ?>
2
  • Right. I put it right after the div nav. It works now. Thanks!
    – woninana
    Jan 30, 2013 at 3:17
  • 1
    Wow! Isn't it exactly what I told you there was wrong with it?
    – istos
    Jan 30, 2013 at 4:40
3

This question was answered very adequately by @Trevor but I needed to implement numbered pagination, and there was a bit more research to do. I hope my code helps others implement numbered pagination.

    <div class="frontpage-posts">
        <?php
        if (get_query_var('paged')) {
          $paged = get_query_var('paged');
        } elseif (get_query_var('page')) {
          $paged = get_query_var('page');
        } else {
          $paged = 1;
        }
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query('posts_per_page=12&paged=' . $paged);
        if ($wp_query->have_posts()) :
          while ($wp_query->have_posts()) : $wp_query->the_post();
            echo the_title();
          endwhile; ?>
          <nav>
            <?php
            the_posts_pagination(array(
              'mid_size'  => 2,
              'prev_text' => __('Back', 'textdomain'),
              'next_text' => __('Onward', 'textdomain'),
            ));
            ?>
          </nav>
        <?php
          $wp_query = null;
          $wp_query = $temp;
          wp_reset_postdata();
        endif;
        ?>
      </div>
0
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type'     => 'wp-rest-api-log',
    'posts_per_page' => 5,
    'paged'         => $paged,
    'orderby'       => 'date',
);

$loop = new WP_Query( $args );

if ($loop->have_posts()) :
    while ($loop->have_posts()) : $loop->the_post();
      echo the_title();
    endwhile;
    
    echo "<nav class=\"sw-pagination\">";
    $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' => $loop->max_num_pages
    ) );
    echo "</nav>";
endif;

wp_reset_query();
-2
<?php    
add_shortcode('show_all_news', 'show_all_news');
    function show_all_news($atts) {
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $limit = 4;
        $offset = ( $limit * $paged ) - $limit;
        $atts['pages'] = $paged; 
        $atts['post-type'] = 'your_custom_post_type'; 
        $atts['orderby'] = 'date'; 
        $atts['order'] = 'DESC';
        $atts['offset'] = $offset; 
        $atts['posts_per_page'] = $limit; 
        $atts['caller_get_posts'] = 1; 

        $result = new WP_Query($atts);
        echo '<div class="news-cont">';  
        if($result->have_posts())
        {
            while ($result->have_posts()) : $result->the_post(); 
                echo '<div class="bg-white news-block">'; 
                echo '<em class="date">'. get_the_date().'</em>' ?>

                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php 
                echo "<p>";
                the_excerpt(); 
                echo "</p>";
                echo '</div>';
            endwhile;

            ?>
                <ul class="pagination" style="width:100%;">
                    <li id="previous-posts" style="float:left">
                        <?php previous_posts_link( '<< Vorige Pagina', $result->max_num_pages ); ?>
                    </li>
                    <li id="next-posts" style="float:right">
                        <?php next_posts_link( 'Volgende Pagina >>', $result->max_num_pages ); ?>
                    </li>
                </ul>
            <?php 

        }
        echo '</div>'; 
        wp_reset_query();
    }
3
  • 1
    suraj sing, Would you like to add some details that you addressed in the answer by updating it?
    – Rao
    Feb 29, 2016 at 7:58
  • This is a working code Just change post type........... add your custom post type and add this code in functions.php and do_shortcode('shortcodename '); in category listing page where you needed pagination...... this will work Mar 2, 2016 at 5:43
  • @surajsingh, Involving short code is something not recommended or necessary at all in this situation. Also, you did not give any information about explaining your solution. Thank you. Dec 19, 2020 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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