Php 不同div中多个循环的分页

Php 不同div中多个循环的分页,php,wordpress,pagination,Php,Wordpress,Pagination,我有一个大问题。是否可以对两个循环使用分页。我的密码是 <div class="first_div"> <?php if (have_posts()) : $count = 0; while (have_posts()) : the_post(); $count++; if ($count == 1) :

我有一个大问题。是否可以对两个循环使用分页。我的密码是

<div class="first_div">
    <?php
        if (have_posts()) :
            $count = 0; 
            while (have_posts()) : the_post();
                $count++;
                if ($count == 1) :
                    the_title();
                elseif ($count == 2) :
                    the_title();
                elseif ($count == 3) :
                    the_title();
                endif;
            endwhile;
        endif;      
    ?>
</div>

<div class="second_div">
    <h3>Div between first_div and third_div</h3>
</div>

<div class="third_div">
    <?php
        query_posts( array('posts_per_page'=>4,'offset'=>3) );
        while ( have_posts() ) : the_post();
            the_title();
        endwhile;
    ?>
</div>

第一分区和第三分区之间的分区

从上面的代码中,我需要显示总共7条最新消息。第一分区3个,第三分区4个,效果很好。所以,现在我需要做的是,我需要在第三个div之后进行分页。但实际上,我需要在第一个div和第三个div之间进行一个div。因此,我无法在第三个div之后创建分页。是否可以如我在上面的评论中所述进行分页,这都可以在一个查询中完成

只是一个重要的注意事项,在我跳进厚厚的东西之前,永远不要使用

注意:此功能不适用于插件或主题。正如后面解释的,有更好、更高性能的选项来更改主查询。query_posts()是一种过于简单且有问题的方法,通过将页面的主查询替换为查询的新实例来修改页面的主查询。它效率低下(重新运行SQL查询),并且在某些情况下会彻底失败(尤其是在处理POST分页时)

(警告:这未经测试,但应该有效)

好的,下面是我们要怎么做

  • 正常运行循环。我们将使用内置循环计数器(
    $current\u post
    ,记住,它从
    0
    ,而不是
    1
    )来计算我们的帖子,然后根据它,做一些事情

  • 在我们的第一次运行中,我们将跳过第4-7篇文章,只显示Div1中的前三篇

  • 在循环的第一次运行之后,我们将在div2中显示分页

  • 为了显示帖子4-7,我们需要倒带我们的循环,并再次运行它

  • 在第二次运行中,我们将跳过前三篇文章,只在第3部分中显示第4-7篇文章

现在,让我们开始编码

1.)正常运行循环,排除/跳过帖子4-7

if( have_posts() ) {
    while( have_posts() ) {
        the_post(); 

        if( 0 === $wp_query->current_post ) {
            echo '<div class="first_div">'; // Open our div container only if it is the first post
        }

        the_title();

        if ( 2 === $wp_query->current_post ) {
            echo '</div>'; // Close our div container after post three
        }

        if( 3 >= $wp_query->current_post ) {
            //finish of the loop but displays nothing from posts 4 - 7
        }

    }
}
if( have_posts() ) {
    while( have_posts() ) {
        the_post(); 

        if( 2 <= $wp_query->current_post ) {
            //Skip posts 4 - 7 and displays nothing
        }

        if( 3 === $wp_query->current_post ) {
            echo '<div class="third_div">'; // Open our div container only if it is the fourth post
        }

        the_title();

        if ( 6 === $wp_query->current_post ) {
            echo '</div>'; // Close our div container after post seven
        }

    }
}
4.)重新运行循环,仅显示第4-7栏

if( have_posts() ) {
    while( have_posts() ) {
        the_post(); 

        if( 0 === $wp_query->current_post ) {
            echo '<div class="first_div">'; // Open our div container only if it is the first post
        }

        the_title();

        if ( 2 === $wp_query->current_post ) {
            echo '</div>'; // Close our div container after post three
        }

        if( 3 >= $wp_query->current_post ) {
            //finish of the loop but displays nothing from posts 4 - 7
        }

    }
}
if( have_posts() ) {
    while( have_posts() ) {
        the_post(); 

        if( 2 <= $wp_query->current_post ) {
            //Skip posts 4 - 7 and displays nothing
        }

        if( 3 === $wp_query->current_post ) {
            echo '<div class="third_div">'; // Open our div container only if it is the fourth post
        }

        the_title();

        if ( 6 === $wp_query->current_post ) {
            echo '</div>'; // Close our div container after post seven
        }

    }
}
if(have_posts()){
while(have_posts()){
_post();
如果(2个当前职位){
//跳过帖子4-7,不显示任何内容
}
如果(3==$wp\U查询->当前\U帖子){
echo“”;//仅当它是第四个post时才打开我们的div容器
}
_title();
如果(6==$wp\U查询->当前\U帖子){
echo“”;//在post 7之后关闭我们的div容器
}
}
}

这应该是

你需要只对前三篇文章或所有7篇文章进行分页。所有7篇文章@pietergoosen然后你可以在一个查询中完成所有这一切。顺便说一句,永远不要使用
query\u posts
。它很容易失败,特别是在分页时。。tk bro感谢您的回复@pieter Goosen仅供参考,了解一下网站的运作情况。如果您需要有关站点的任何帮助,请参阅::-)