繁体   English   中英

Wordpress 分页问题 - 我设置了“posts_per_page”,它适用于每个页面,除了第 1 页,它只显示所有帖子

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

我的模板文件中有以下设置:

<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(); ?>

此功能有效,除了第一页。 如“posts_per_page”中所指定,每隔一页将看到的帖子数量限制为 12,但第 1 页仅显示每个帖子。 我试过四处寻找类似的问题,但都失败了。 对于这里可能发生的事情,我将不胜感激,谢谢!

您可以在此处查看问题: http://listingmirror.devsite.work/blog/

尝试这种形式

    // 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();

看起来只需要离开一天。 找错地方了。 我完全忽略了从客户端导入的帖子在 Wordpress 中都被标记为“粘性”。 这是覆盖 post_per_page 参数。 现在已经解决了,但我想把解决方案放在这里,以防有人遇到同样的问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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