使用此循环的 Wordpress 分页

Wordpress pagination with this loop

你能帮我找到正确的循环分页代码吗? 这是我的循环:

<?php query_posts('showposts=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; else: ?>
...
<?php endif; ?>
<?php rewind_posts(); ?>
<?php query_posts('showposts=2&offset=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; else: ?>
...
<?php endif; ?>
<?php rewind_posts(); ?>
<?php query_posts('showposts=6&offset=3&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; else: ?>
...
<?php endif; ?>

编辑

这是我的旧循环,如何将其转换为新循环?不幸的是我不知道 php 是如何工作的..

<?php 
   if (is_front_page() ) {
           get_header( 'front' );

    } else {
           get_header();
   }
?>

<section id="content" class="container">
<div class="row">
    <div class="col-md-7 col-sm-7">
<?php query_posts('showposts=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <article class="thumbnail thumbnail-principale expand-image">
            <div class="featured-image">
                <a href="<?php the_permalink(); ?>" title="<?php    the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
            </div>
            <div class="destacado"><h3>Destacado</h3></div>
                <header class="testo-articolo">
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="entry-meta">
                        <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
                    </div>
                    <p><?php the_excerpt(); ?></p>
                    <?php comments_template(); ?><!-- da sistemare -->
                </header>
        </article>
    </div>

<?php endwhile; else: ?>

<p>Sorry, there are no posts to display</p>

<?php endif; ?>

<?php rewind_posts(); ?>
<?php query_posts('showposts=2&offset=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="col-md-5 col-sm-5">
        <article class="thumbnail thumbnail-destra expand-image">
            <div class="featured-image">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
            </div>
            <header class="testo-articolo-destra expand-image">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="entry-meta">
                    <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
                </div>
                <p><?php the_excerpt(); ?></p>
            </header>
            <div class="badge1"></div>
        </article>
    </div>
<?php endwhile; else: ?>

<p>Sorry, there are no posts to display</p>

<?php endif; ?>
</div><!-- /.row -->
<div class="row">
<?php rewind_posts(); ?>
<?php query_posts('showposts=6&offset=3&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <div class="col-md-4 col-sm-4">
        <article class="thumbnail distanza expand-image">
            <div class="featured-image">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
            </div>
            <header class="testo-articolo">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="entry-meta">
                    <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
                </div>
                <p><?php the_excerpt(); ?></p>
                <p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
            </header>
        </article>
    </div>

<?php endwhile; else: ?>

<p>Sorry, there are no posts to display</p>

<?php endif; ?>


</div><!-- /row -->
<div style="text-align:center;">
    <?php posts_nav_link(‘|’, ‘Prossimo’, ‘Precedente’); ?>
</div>
</section>
</div><!-- /sezione -->
<?php get_footer(); ?>

正如我昨晚所说,您可以在一个查询中执行此操作,而无需自定义查询。

我想强调的是,您应该永远不要使用query_postsquery_posts 破坏分页,重新运行 查询会降低页面性能并破坏许多插件或自定义函数所需的主要查询对象。这确实是 运行 自定义查询的一种邪恶方式,应该不惜一切代价避免。如果您需要 运行 自定义查询,请对分页查询使用 WP_Query,对非分页查询使用 get_posts

您也不应将主页和任何存档类型页面上的主查询替换为自定义查询,这会破坏页面功能。自定义查询只应 运行 用于次要 post,例如滑块、特色内容、相关和流行的 post,以及页面模板和静态首页以显示自定义 post .如果您需要更改主页或存档页面上的主要查询,请使用 pre_get_posts

现在,要解决您的问题,您需要一个来自主查询的循环和一个计数器,在这种情况下,我们将使用内置主查询计数器 $wp_query->current_post(请注意,此计数器从 0,因此 post 循环中的一个将是 0,post 两个将是 1,等等)。

根据我从你的代码中读到的内容,第一个代码需要不同于 post 第二个和第三个,并且这些不同于接下来的六个

让我们将其写入代码

if ( have_posts() ) {

    while ( have_posts() ) {
        the_post();

            /*
             * Display different outlay for post one
             */
        if ( $wp_query->current_post === 0 ) {

            // Do what needs to be done for post one

        } elseif ( $wp_query->current_post >= 1 && $wp_query->current post <= 2 ) {

            /* 
             * Do something for posts 2 and 3
             */

        } elseif ( $wp_query->current_post >= 3 ) {

            /*
             * Do something for the rest of the posts
             */

        }
    }

}

编辑

您可以将代码转换为

<?php 
   if (is_front_page() ) {
           get_header( 'front' );

    } else {
           get_header();
   }
?>

<section id="content" class="container">
<div class="row">
    <div class="col-md-7 col-sm-7">

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
 if ( $wp_query->current_post === 0 ) { ?>
        <article class="thumbnail thumbnail-principale expand-image">
            <div class="featured-image">
                <a href="<?php the_permalink(); ?>" title="<?php    the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
            </div>
            <div class="destacado"><h3>Destacado</h3></div>
                <header class="testo-articolo">
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="entry-meta">
                        <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
                    </div>
                    <p><?php the_excerpt(); ?></p>
                    <?php comments_template(); ?><!-- da sistemare -->
                </header>
        </article>
    </div>

<?php

} elseif ( $wp_query->current_post >= 1 && $wp_query->current post <= 2 ) { ?>

    <div class="col-md-5 col-sm-5">
        <article class="thumbnail thumbnail-destra expand-image">
            <div class="featured-image">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
            </div>
            <header class="testo-articolo-destra expand-image">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="entry-meta">
                    <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
                </div>
                <p><?php the_excerpt(); ?></p>
            </header>
            <div class="badge1"></div>
        </article>
    </div>
<?php 

} elseif ( $wp_query->current_post >= 3 ) { ?>

    <div class="col-md-4 col-sm-4">
        <article class="thumbnail distanza expand-image">
            <div class="featured-image">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
            </div>
            <header class="testo-articolo">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="entry-meta">
                    <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
                </div>
                <p><?php the_excerpt(); ?></p>
                <p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
            </header>
        </article>
    </div>

<?php }

endwhile; 
endif; ?>


</div><!-- /row -->
<div style="text-align:center;">
    <?php posts_nav_link(‘|’, ‘Prossimo’, ‘Precedente’); ?>
</div>
</section>
</div><!-- /sezione -->
<?php get_footer(); ?>

不知道这样对不对,我明白了:

<?php 
if (is_front_page() ) {
       get_header( 'front' );

} else {
       get_header();
}
?>

<section id="content" class="container">
<div class="row">


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
if (!is_paged() && $wp_query->current_post === 0 ) { ?>
<div class="col-md-7 col-sm-7">
    <article class="thumbnail thumbnail-principale expand-image">
        <div class="featured-image">
            <a href="<?php the_permalink(); ?>" title="<?php    the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
        </div>
        <div class="destacado"><h3>Destacado</h3></div>
            <header class="testo-articolo">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="entry-meta">
                    <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
                </div>
                <p><?php the_excerpt(); ?></p>
                <?php comments_template(); ?><!-- da sistemare -->
            </header>
    </article>
</div>

<?php

} elseif (!is_paged() &&  $wp_query->current_post >= 1 && $wp_query->current_post <= 2 ) { ?>

<div class="col-md-5 col-sm-5">
    <article class="thumbnail thumbnail-destra expand-image">
        <div class="featured-image">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive')); ?></a>
        </div>
        <header class="testo-articolo-destra expand-image">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="entry-meta">
                <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('0', '1 Comentario', '% Comentarios'); ?></span></p>
            </div>
            <p><?php the_excerpt(); ?></p>
        </header>
        <div class="badge1"></div>
    </article>
</div>
<?php 

} elseif ( $wp_query->current_post >= 3 ) { ?>

<div class="col-md-4 col-sm-4">
    <article class="thumbnail distanza expand-image">
        <div class="featured-image">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
        </div>
        <header class="testo-articolo">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="entry-meta">
                <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
            </div>
            <p><?php the_excerpt(); ?></p>
            <p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
        </header>
    </article>
</div>

<?php 
//new request for all the rest of post from page 2 onwards
} elseif ( is_paged() ) { ?>

<div class="col-md-4 col-sm-4">
    <article class="thumbnail distanza expand-image">
        <div class="featured-image">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-responsive ingrandire-img')); ?></a>
        </div>
        <header class="testo-articolo">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="entry-meta">
                <p class="text-muted resume"><span><i class="fa fa-calendar"></i><?php the_time('j M y'); ?></span><span><i class="glyphicon glyphicon-user"></i><?php the_author(); ?></span><span><i class="fa fa-comment-o"></i><?php comments_popup_link('Ningún comentario', '1 Comentario', '% Comentarios'); ?></span></p>
            </div>
            <p><?php the_excerpt(); ?></p>
            <p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e( 'Lee más', 'katartika' ); ?></a></p>
        </header>
    </article>
</div>

<?php }

endwhile; 
endif; ?>


</div><!-- /row -->
<div style="text-align:center;">
<?php posts_nav_link('|', 'Prossimo', 'Precedente'); ?>
</div>
</section>
</div><!-- /sezione -->
<?php get_footer(); ?>