Blog posts pagination not working

The question:

I have been debugging this but I cannot seem to find what’s wrong. An extra eye would help!
I am trying to add pagination on my blog posts. Here is the code

On home.php

 <?php

    $total_pages = $wp_query->max_num_pages;

    if ( $total_pages > 1 ) : ?>
        <div class="pagination col-12 text-center mt-3">

            <?php the_posts_navigation(); ?>

        </div>

    <?php endif; ?>

And on template-tags.php

if ( ! function_exists( 'the_posts_navigation' ) ) :
function the_posts_navigation() {

    global $wp_query;

    echo paginate_links( array(
        'base' => get_permalink() . 'page/%#%' . '/', 'format' => '?paged=%#%',
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'prev_text' => return_svg( 'arrow-left' ),
        'next_text' => return_svg( 'arrow-right' ),
    ) );
}
endif;

Any idea what seems to be wrong? Thank you so much!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The most common problem of similar questions I see on WPSE – contributors confused about template hierarchy.
In your case,
it’s better to check Common WordPress template files section here

These items most important for your question:

front-page.php
The front page template is always used as the site front page if it exists, regardless of what settings on Admin > Settings > Reading.
home.php
The home page template is the front page by default. If you do not set WordPress to use a static front page, this template is used to show latest posts.
index.php
The main template file. It is required in all themes.

So, front-page.php used first, then home.php(if it exists in your theme and Admin>Settings>Reading is set to “Your latest posts”), then index.php.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment