Pagination on a custom wp_query post

<?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: ?> 

The URL on the next page as you type the result: www.mywebsite.com/blog/page/2 - WORK. But I can not show links to pages.

Where did it go wrong?

EDIT: The pagination link appears in page/2/ but not on the main page of the blog. Why?

+6
source share
3 answers

I think you put <?php wp_reset_query(); ?> <?php wp_reset_query(); ?> to the wrong place .. should it not be next to or after the pagination codes?

something like that

 <?php endwhile; ?> <?php else: ?> <?php wp_reset_query(); ?> 
0
source

There are three ways I would suggest pagination with a custom wp_query message. Unfortunately, to this day there is not much good information about this, or at least in some cases this is unclear. Hope this helps!

Please note: you also have wp_reset_postdata () in the wrong place, but you need even more to get it working correctly.

Option 1 - use the variable max_num_pages

 <?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 will see above, a slightly different format for previous_posts_link and next_posts_link , which now access the variable max_num_pages . Be sure to use your own query variable name when accessing max_num_pages . Notice that I'm using $ cpt_query, as this is a variable for my query.

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

This is what many recommend, but be careful to assign the $ wp_query variable to the temp variable and reassign it, or you will run into all the trouble. That's why I recommend option number 1. As noted in 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 the WP-pagenavi plugin

As another option, you can use the WP-pagenavi plugin and configure your request, as in Option No. 1. But make one change to the code, delete everything inside the element and replace it with this function as soon as you install the plugin. So you are done:

 <nav> <?php wp_pagenavi( array( 'query' => $cpt_query ) ); ?> </nav> 
+13
source
 <?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(); } 
-2
source

All Articles