「ポートフォリオ」カスタム投稿タイプの 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>
私は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 と効果的に通信していることを確認するだけでも課題が増えます。
助けてくれてありがとう!