I want to query the WP loop for my ‘portfolio’ custom post type and separate the returned portfolio posts into groups of 3 (i.e. wrap every 3 posts in a div).
Then I want to wrap every 2 groups of 3 in another div.
For example, if there were a total of 11 portfolio posts, my desired html output for this query would look like this:
// 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>
I am new to PHP, so I’m trying to piece together some code from other similar scenarios, but I couldn’t find any thread that addressed my specific issue, so I’m unsure if what I’m doing is right. I’m pretty lost
Here is what I’ve tried:
<?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>';
?>
The output looks like this:
<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>
Can anyone make sense of this? The logic is a challenge for me, but also just getting the syntax correct and making sure that the code is talking to WP effectively adds to the challenge.
Thank you in advance for any help!
With the help of @jothikannan and his instruction to include my
$i++
increment counter inside thewhile
loop, I also discovered that I needed to include the closingecho '</article>';
inside thewhile
loop.So here is the final code:
And this successfully outputs the following html for my portfolio archive page (there are only 8 portfolio posts in my database for this example):
Success! Thank you!
Try to make increment on inside the while loop ,