Php 如何将自定义帖子类型WP查询中的帖子分成3组,然后将这些3组的帖子分成2组?

Php 如何将自定义帖子类型WP查询中的帖子分成3组,然后将这些3组的帖子分成2组?,php,counter,custom-post-type,wordpress,Php,Counter,Custom Post Type,Wordpress,我想查询我的“公文包”自定义帖子类型的WP循环,并将返回的公文包帖子分成3组(即每3篇帖子包装一个div) 然后,我想把每2组3个放在另一个div中 例如,如果总共有11篇公文包文章,我希望此查询的html输出如下所示: // This is the HTML I'd like to generate on my portfolio posts archive page. This is assuming there are a total of 11 posts in the database

我想查询我的“公文包”自定义帖子类型的WP循环,并将返回的公文包帖子分成3组(即每3篇帖子包装一个div)

然后,我想把每2组3个放在另一个div中

例如,如果总共有11篇公文包文章,我希望此查询的html输出如下所示:

// This is the HTML I'd like to generate on my portfolio posts archive page. This is assuming there are a total of 11 posts in the database:

<div id="page_wrap">

<div id="wrap_6_posts">

    <div id="wrap_3_posts" class="top-row">
        <article class="portfolio-post FIRST"> Post 1 </article>
        <article class="portfolio-post"> Post 2 </article>
        <article class="portfolio-post"> Post 3 </article>
    </div>

    <div id="wrap_3_posts" class="bottom-row">
        <article class="portfolio-post FIRST"> Post 4 </article>
        <article class="portfolio-post"> Post 5 </article>
        <article class="portfolio-post"> Post 6 </article>
    </div>

</div>

<div id="wrap_6_posts">

    <div id="wrap_3_posts" class="top-row">
        <article class="portfolio-post FIRST"> Post 7 </article>
        <article class="portfolio-post"> Post 8 </article>
        <article class="portfolio-post"> Post 9 </article>
    </div>

    <div id="wrap_3_posts" class="bottom-row">
        <article class="portfolio-post FIRST"> Post 10 </article>
        <article class="portfolio-post"> Post 11 </article>
    </div>

</div>
<div id="page_wrap">
<div id="wrap_6_posts">
    <div id="wrap_3_posts" class="top-row">
        <article class="portfolio-post first">      
            post 1

        <article class="portfolio-post first">      
            post 2

        <article class="portfolio-post first">      
            post 3

        <article class="portfolio-post first">      
            post 4

        <article class="portfolio-post first">      
            post 5

        <article class="portfolio-post first">      
            post 6

        <article class="portfolio-post first">      
            post 7

        <article class="portfolio-post first">      
            post 8

        <article class="portfolio-post first">      
            post 9

        <article class="portfolio-post first">      
            post 10

        <article class="portfolio-post first">      
            post 11
        </article>

    </div>
</div>
//这是我想在公文包帖子归档页面上生成的HTML。这是假设数据库中总共有11个员额:
输出如下所示:

// This is the HTML I'd like to generate on my portfolio posts archive page. This is assuming there are a total of 11 posts in the database:

<div id="page_wrap">

<div id="wrap_6_posts">

    <div id="wrap_3_posts" class="top-row">
        <article class="portfolio-post FIRST"> Post 1 </article>
        <article class="portfolio-post"> Post 2 </article>
        <article class="portfolio-post"> Post 3 </article>
    </div>

    <div id="wrap_3_posts" class="bottom-row">
        <article class="portfolio-post FIRST"> Post 4 </article>
        <article class="portfolio-post"> Post 5 </article>
        <article class="portfolio-post"> Post 6 </article>
    </div>

</div>

<div id="wrap_6_posts">

    <div id="wrap_3_posts" class="top-row">
        <article class="portfolio-post FIRST"> Post 7 </article>
        <article class="portfolio-post"> Post 8 </article>
        <article class="portfolio-post"> Post 9 </article>
    </div>

    <div id="wrap_3_posts" class="bottom-row">
        <article class="portfolio-post FIRST"> Post 10 </article>
        <article class="portfolio-post"> Post 11 </article>
    </div>

</div>
<div id="page_wrap">
<div id="wrap_6_posts">
    <div id="wrap_3_posts" class="top-row">
        <article class="portfolio-post first">      
            post 1

        <article class="portfolio-post first">      
            post 2

        <article class="portfolio-post first">      
            post 3

        <article class="portfolio-post first">      
            post 4

        <article class="portfolio-post first">      
            post 5

        <article class="portfolio-post first">      
            post 6

        <article class="portfolio-post first">      
            post 7

        <article class="portfolio-post first">      
            post 8

        <article class="portfolio-post first">      
            post 9

        <article class="portfolio-post first">      
            post 10

        <article class="portfolio-post first">      
            post 11
        </article>

    </div>
</div>

职位1
职位2
职位3
邮政4
邮政5
邮政6
邮政7
邮政8
邮政9
10号岗位
11号岗位

有人能理解这一点吗?对我来说,逻辑是一个挑战,但仅仅是获得正确的语法,并确保代码与WP有效对话,就增加了挑战


提前感谢您的帮助

尝试在while循环内增加

    <?php
$args = array( 'post_type' => 'portfolio' );
$loop = new WP_Query( $args );
$i = 0;

echo'<div id="wrap_6_posts">' . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";

while ( $loop->have_posts() ) : $loop->the_post();

    if ($i % 6 == 0 && $i > 0) {
        echo '</div>' . "\n" . '</div>' . "\n" . '<div id="wrap_6_posts">'  . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";
    } else if ($i % 3 == 0 && $i > 0) {
        echo '</div>' . "\n" . '<div id="wrap_3_posts" class="bottom-row">' . "\n";
    }

echo '<article class="portfolio-post' . ($i % 3 == 0 ? ' first' : '') . '">' . "\n";

?>
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php

echo '</article>' . "\n";
$i++;

endwhile;

echo '</div>' . "\n" . '</div>';
?>

尝试在while循环中增加

    <?php
$args = array( 'post_type' => 'portfolio' );
$loop = new WP_Query( $args );
$i = 0;

echo'<div id="wrap_6_posts">' . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";

while ( $loop->have_posts() ) : $loop->the_post();

    if ($i % 6 == 0 && $i > 0) {
        echo '</div>' . "\n" . '</div>' . "\n" . '<div id="wrap_6_posts">'  . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";
    } else if ($i % 3 == 0 && $i > 0) {
        echo '</div>' . "\n" . '<div id="wrap_3_posts" class="bottom-row">' . "\n";
    }

echo '<article class="portfolio-post' . ($i % 3 == 0 ? ' first' : '') . '">' . "\n";

?>
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php

echo '</article>' . "\n";
$i++;

endwhile;

echo '</div>' . "\n" . '</div>';
?>

在@jothikannan的帮助下,他指示在
循环中包含我的
$i++
增量计数器,而
循环中,我还发现我需要包含关闭
回音'
中,而
循环

这是最后的代码:

<?php
$args = array( 'post_type' => 'portfolio' );
$loop = new WP_Query( $args );
$i = 0;

echo'<div id="wrap_6_posts">' . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";

while ( $loop->have_posts() ) : $loop->the_post();

    if ($i % 6 == 0 && $i > 0) {
        echo '</div>' . "\n" . '</div>' . "\n" . '<div id="wrap_6_posts">'  . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";
    } else if ($i % 3 == 0 && $i > 0) {
        echo '</div>' . "\n" . '<div id="wrap_3_posts" class="bottom-row">' . "\n";
    }

echo '<article class="portfolio-post' . ($i % 3 == 0 ? ' first' : '') . '">' . "\n";

?>
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php

echo '</article>' . "\n";
$i++;

endwhile;

echo '</div>' . "\n" . '</div>';
?>

在@jothikannan的帮助下,他指示在
循环中包含我的
$i++
增量计数器,而
循环中,我还发现我需要包含关闭
回音'
中,而
循环

这是最后的代码:

<?php
$args = array( 'post_type' => 'portfolio' );
$loop = new WP_Query( $args );
$i = 0;

echo'<div id="wrap_6_posts">' . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";

while ( $loop->have_posts() ) : $loop->the_post();

    if ($i % 6 == 0 && $i > 0) {
        echo '</div>' . "\n" . '</div>' . "\n" . '<div id="wrap_6_posts">'  . "\n" . '<div id="wrap_3_posts" class="top-row">' . "\n";
    } else if ($i % 3 == 0 && $i > 0) {
        echo '</div>' . "\n" . '<div id="wrap_3_posts" class="bottom-row">' . "\n";
    }

echo '<article class="portfolio-post' . ($i % 3 == 0 ? ' first' : '') . '">' . "\n";

?>
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php

echo '</article>' . "\n";
$i++;

endwhile;

echo '</div>' . "\n" . '</div>';
?>

尝试在while循环$i++内增加;尝试在while循环$i++内增加;百分比是多少?等于?你是什么意思?我拿不到你的最后一个comment@BenRacicot百分比符号是模数运算符。请参阅@jothikannan谢谢你发现了这个错误。根据您的建议和一个小小的调整(包括while循环中的closing/article容器),我终于获得了正确的输出!谢谢jothikannan!我将在下面发布完整的答案。@jothikannan它几乎是正确的。这让我找到了正确的答案。但我不想让这篇文章的未来读者感到困惑,所以我在下面发布了完整的正确答案。我提到你的回答很有帮助。百分比是多少?等于?你是什么意思?我拿不到你的最后一个comment@BenRacicot百分比符号是模数运算符。请参阅@jothikannan谢谢你发现了这个错误。根据您的建议和一个小小的调整(包括while循环中的closing/article容器),我终于获得了正确的输出!谢谢jothikannan!我将在下面发布完整的答案。@jothikannan它几乎是正确的。这让我找到了正确的答案。但我不想让这篇文章的未来读者感到困惑,所以我在下面发布了完整的正确答案。我提到你的回答很有帮助。