Home > Enterprise >  Wordpress pagination working only on one page
Wordpress pagination working only on one page

Time:10-01

I have some pagination after a loop. It works in front-page.php, but not other custom pages, even though it’s the same thing in both.

<?php
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;

    $args = array(
    'post_type' => 'post',
    'posts_per_page' => get_option( 'posts_per_page' ),
    'paged'=> $paged,
    'post_status' => 'publish',
    'category_name' => 'home',
    );
    $arr_posts = new WP_Query( $args );

    if ( $arr_posts->have_posts() ) :
        while ( $arr_posts->have_posts() ) : $arr_posts->the_post(); ?>

            <div >
                <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'large', array('class' => 'first-image') ); } ?>
                <h1 ><?php the_title(); ?></h1>
                <?php the_content(); ?>
            </div>

        <?php endwhile; ?>
    <?php endif; ?>

<div >
    <nav class='pagination'>
        <div >
            <?php previous_posts_link( '<span><svg id="Ebene_2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 211.41 211.41"><defs><style>.cls-1{fill:#fff;}</style></defs><g id="Ebene_1-2"><path  d="M0,0H211.41V11.75H20.03L155.36,147.08l-8.28,8.28L11.75,20.03V211.41H0V0Z"/></g></svg></span><span >newer&nbsp;posts</span>' ); ?>
        </div>
        <div >
            <?php next_posts_link( '<span >older&nbsp;posts</span><span><svg id="Ebene_2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 211.41 211.41"><defs><style>.cls-1{fill:#fff;}</style></defs><g id="Ebene_1-2"><path  d="M0,199.67H191.39L56.06,64.33l8.28-8.28L199.67,191.39V0h11.75V211.41H0v-11.75Z"/></g></svg></span>' ); ?>
        </div>
    </nav>
</div>

I’ve tried changing 'paged' to 'page', other functions and more. At first, nothing I tried even worked on the main page, they just didn’t show up in the DOM. But then I copied some different pagination functions around and suddenly every function showed some result. I have no idea why it works, but I went with it, and now I have the same problem on the other pages. No function I try even shows up in the DOM. I know this has been asked before, but I just can’t figure it out, even with other examples. Thanks!

CodePudding user response:

It seems that you have not added the second parameter in to the function next_posts_link . Just add it and it will start working See the below example next_posts_link( 'Older Entries', $arr_posts->max_num_pages );

  • Related