<?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 »' ); ?>
<?php next_posts_link('Older »') ?>
</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?
Pagination Note: You should set get_query_var( 'page' ); if you want your query to work with pagination. Since WordPress 3.0. 2, you do get_query_var( 'page' ) instead of get_query_var( 'paged' ). The pagination parameter 'paged' for WP_Query() remains the same.
In order to add pagination to a WordPress theme, we need to build a function which will output previous and next post links at the bottom of the page, then add that to our template page. This is similar to the “Older Entries” and “Newer Entries” links that we saw above.
WordPress provides several functions for automatically displaying a numerical pagination list. If you want more robust pagination options, you can use the_posts_pagination() for WordPress 4.1 and higher. This will output a set of page numbers with links to previous and next pages of posts.
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(); ?>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With