I’m having trouble being able to insert an ad after every 3rd post in WordPress. This is the code in the theme. I know I need a counter and an if statement.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('recent'); ?></a>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
<a id="loadmore" href="#" data-loading="<?php _e( 'Loading ...', THB_THEME_NAME ); ?>" data-nomore="<?php _e( 'No More Posts to Show', THB_THEME_NAME ); ?>" data-count="5" data-action="thb_ajax_home"><?php _e( 'Load More', THB_THEME_NAME ); ?></a>
</section>
I could show you what I tried and failed at, just to show that I tried. Don’t give me negative points for that please.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php $i = 1; if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); if($i == 1) : ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('recent'); ?></a>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<div class="clear"> </div>
<?php if ( $i == 3|| $i == 9 || $i == 15 ) : ?>
<?php if (function_exists ('adinserter')) echo adinserter (2); ?>
<div class="clear"> </div>
<?php endif; ?><?php endif; ?>
<?php $i++; ?>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
</section>
Thank you.
You’re close. You’ll want to increment your counter within the
while
loop (using$i++
), and then use the modulus operator%
to determine whether or not the counter is evenly divisible by 3: