簡體   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