Reputation: 11
I'm trying to figure out if there's a way to add a section of text after 4 posts on my index.php page? And then again at 8 posts.
I've only got a simple loop at the moment and haven't known where to start! Thanks
<?php
get_header();
?>
<main id="primary" class="site-main">
<div class="intro">
<h1 class="intro-text">Hi! I’m an Aussie illustrator living in London.</h1>
</div>
<div class="post-grid grid">
<div class="grid-sizer">
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content-thumb', get_post_type() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div>
</div>
</main><!-- #main -->
<?php
get_footer();
Upvotes: 0
Views: 40
Reputation: 2214
Try this code which uses the modulus division operator that checks for the Nth iteration.
EDIT: I moved $counter++; to the top of the while loop. It should work properly now.
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
$counter++;
the_post();
get_template_part( 'template-parts/content-thumb', get_post_type() );
// Check for 4th iteration in the loop
if ($counter % 4 == 0) {
echo 'YOUR TEXT HERE';
}
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
Upvotes: 1