0

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!

14
  • 1) Are you sure your blog page uses home.php template? 2) On template-tags.php you check if the_post_navigation exists, but it always exists, because it's wordpress function. Do you call it if it exists?
    – anton
    Commented May 20, 2021 at 8:55
  • Hi @anton , yes blog page uses home.php. I am trying to make global pagination. What would you suggest? Even if I remove the if exists condition, pagination won't still show.
    – ddd
    Commented May 20, 2021 at 9:08
  • I don't think problem related to the_posts_navigation(), because it's native wp function. And I can't see your pages full code. Settings -> Reading -> Your home page displays is set to "Your latest posts"?
    – anton
    Commented May 20, 2021 at 9:17
  • Oh, home.php is automatically used on the posts page. This is my reference: wphierarchy.com @anton, I still cannot see why the pagination is not showing. Blogs are showing though. Something wrong with my if statement on displaying pagination?
    – ddd
    Commented May 20, 2021 at 9:26
  • In your first code, you don't need to check total pages (the same check goes inside already), unless you need to hide wrapper div for navigation. Try to echo something in your home.php, just to make sure you see it on your home page. I asked this before, because home.php is used when "Your latest posts" is checked.
    – anton
    Commented May 20, 2021 at 9:43

1 Answer 1

0

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.

1
  • Thanks much @anton!
    – ddd
    Commented May 20, 2021 at 11:13

Your Answer

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

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