In my wordpress loop, i have added pagination everything is working, except when i navigate to last page.
I have a total of 16 posts and I am displaying 6 posts per page, i can navigate to pages 1 and 2, but when navigate to page 3, that should have contain 4 posts it gives me ‘Page not found’ error.
Can anyone, point out what i am doing wrong, below is my code.
<div class="posts-container">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'date',
'posts_per_page' => 6,
'paged' => $paged
);
$rainbow_posts = new WP_Query($args);
if($rainbow_posts->have_posts()):
while($rainbow_posts->have_posts()):
$rainbow_posts->the_post();
$post_url = get_permalink();?>
<div class="post">
<div class="featured-img">
<a href="<?=$post_url;?>">
<picture>
<img src="<?=get_the_post_thumbnail_url();?>" alt="blog-featured-img" width=387 height=258>
</picture>
</a>
</div>
<div class="categories">
<span>
<?php
$categories = get_the_category();
foreach ( $categories as $category ) :?>
<a href="<?=esc_url( get_category_link( $category->term_id ) ); ?>"><?=esc_html( $category->name );?></a>
<?php
if($category !== end($categories)):?>
|
<?php
endif;
endforeach;?>
</span>
</div>
<h6><a href="<?=$post_url;?>"><?=esc_html(get_the_title());?></a></h6>
<div class="content">
<?php
$full_content = get_the_content();
$trimmed_content = wp_trim_words( $full_content, 20, '...' );
echo $trimmed_content;
?>
</div>
<div class="avatar">
<div class="first-column">
<picture>
<source media="(max-width:576px)" srcset="<?=get_avatar_url( get_the_author_meta( 'ID' ), array( 'size' => 50 ) );?>" width=50 height=50>
<img src="<?=get_avatar_url( get_the_author_meta( 'ID' ), array( 'size' => 100 ) );?>" width=64 height=62 alt="author">
</picture>
</div>
<div class="second-column">
<p><?=get_the_author();?></p>
<p><?=get_the_modified_date('d M Y').', '.do_shortcode('[rt_reading_time]').' min read';?></p>
</div>
</div>
</div>
<?php
endwhile;?>
<div class="pagination">
<?=paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => max(1, get_query_var('paged')),
'total' => $rainbow_posts->max_num_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));?>
</div>
<?php wp_reset_postdata();
endif;?>
</div>
Any help would be greatly appreciated.