简体   繁体   中英

Wordpress pagination issue - I set 'posts_per_page' which works for every page except for page 1 which is just showing all posts

I have the following set up in my template file:

<div class="flex" id="allPosts">
        <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array (
        'posts_per_page'    => 12,
        'paged'             => $paged,
        'order'             => 'DESC',
    );
    $wp_query = new WP_Query( $args ); ?>

        <?php if ( $wp_query->have_posts() ) : ?>
        <?php while ($wp_query -> have_posts()) : $wp_query -> the_post(); ?>
        <div class="blog-entries">
            <?php if (has_post_thumbnail()) { 
            $featuredImg = get_the_post_thumbnail_url();
        } else {
            $featuredImg = get_stylesheet_directory_uri() . '/img/blk-fri.jpg';
        }  ?>

            <a class="blog-module" href="<?php the_permalink(); ?>">
                <div class="blog-img" style="background-image: url('<?php echo $featuredImg; ?>');">
                    <div>
                        <div class="author">By <?php the_author(); ?></div>
                        <div class="date">
                            <p><?php echo get_the_date('M'); ?></p>
                            <p><?php echo get_the_date('d'); ?></p>
                        </div>
                    </div>
                </div>
                <div class="blog-content">
                    <h4><?php the_title(); ?></h4>
                    <?php the_excerpt(); ?>
                </div>
            </a>

        </div>

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

    </div>
    <div class="pagination">
        <?php 
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $wp_query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
            'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
            'add_args'     => false,
            'add_fragment' => '',
        ) );
    ?>
    </div>

    <?php wp_reset_postdata(); ?>

This functionality is working, except on page one. Every other page limits the amount of posts seen to 12 as specified in the 'posts_per_page', but page 1 is just showing every post. Ive tried searching around for a similar problem, but have come up short. I would appreciate any and all guidance on what might be happening here, thanks!

You can view the issue here: http://listingmirror.devsite.work/blog/

try in that form

    // WP_Query arguments
$args = array(
    'nopaging'               => false,
    'paged'                  => '12',
    'posts_per_page'         => '12',
    'posts_per_archive_page' => '12',
    'order'                  => 'DESC',
    'orderby'                => 'title',
);

// The Query
$paged = new WP_Query( $args );

// The Loop
if ( $paged->have_posts() ) {
    while ( $paged->have_posts() ) {
        $paged->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

Looks like a case of just needing to step away for a day. Looked in all the wrong places. I completely overlooked that the imported posts from the client were all marked 'sticky' in Wordpress. This was overwriting the post_per_page argument. This is resolved now, but I wanted to put the resolution here in case anyone ever runs into the same issue!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM