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


How do I separate the posts in a custom post type WP query into groups of 3, and then separate those groups of 3 into sets of 2?

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

然后我想把每2组3个包裹在另一个分区中。

例如,如果总共有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>

我是PHP的新手,所以我试图从其他类似的场景中拼凑一些代码,但我找不到任何解决我特定问题的线程,所以我不确定我所做的是否正确。我很失落

以下是我尝试过的:

<?php
$args = array( 'post_type' => 'portfolio' );
$loop = new WP_Query( $args );
$i = 0;
echo'<div id="page_wrap"><div id="wrap_6_posts"><div id="wrap_3_posts" class="top-row">';
while ( $loop->have_posts() ) : $loop->the_post();
    if ($i % 6 == 0 && $i > 0) {
        echo '</div></div><div id="wrap_6_posts"><div id="wrap_3_posts" class="top-row">';
    } else if ($i % 3 == 0 && $i > 0) {
        echo '</div><div id="wrap_3_posts" class="bottom-row">';
    }
    echo '<article class="portfolio-post' . ($i % 3 == 0 ? ' first' : '') . '">';
?>
        <h2 class="headline portfolio-headlines" rel="bookmark">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </h2>
<?php
endwhile;
    echo '</article>';
    $i++;
echo '</div></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>

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

提前感谢您的帮助!

在@jothikannan的帮助下,以及他在while循环中包含我的$i++增量计数器的指示下,我还发现我需要在while循环中包含关闭的echo '</article>';

这是最后的代码:

<?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>';
?>

这成功地为我的公文包归档页面输出了以下html(在这个例子中,我的数据库中只有8个公文包帖子):

<div id="wrap_6_posts">
<div id="wrap_3_posts" class="top-row">
<article class="portfolio-post first">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-1/">Post 1</a>
</h2>
</article>
<article class="portfolio-post">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-2/">Post 2</a>
</h2>
</article>
<article class="portfolio-post">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-3/">Post 3</a>
</h2>
</article>
</div>
<div id="wrap_3_posts" class="bottom-row">
<article class="portfolio-post first">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-4/">Post 4</a>
</h2>
</article>
<article class="portfolio-post">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-5/">Post 5</a>
</h2>
</article>
<article class="portfolio-post">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-6/">Post 6</a>
</h2>
</article>
</div>
</div>
<div id="wrap_6_posts">
<div id="wrap_3_posts" class="top-row">
<article class="portfolio-post first">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-7/">Post 7</a>
</h2>
</article>
<article class="portfolio-post">
<h2 class="headline portfolio-headlines" rel="bookmark">
    <a href="http://example.com/portfolio/post-8/">Post 8</a>
</h2>
</article>
</div>
</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>';
?>