How to insert a div after the third post of this wordpress loop (code below) ??
I searched the site but I couldn’t find an explanation of how to insert it in a code like mine.
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'orderby' => 'rand',
'showposts'=>6, //
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div id="loopindex">';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<div class="post_mobile" id="post-<?php the_ID(); ?>">
CONTENT
</div>
<?php
}
echo '</div>';
}
}
?>
2
Answers
All you need is a
counter
! Before yourwhile loop
starts, initialize a counter variable.Then at the beginning of your
while loop
check the counter value, and at the end ofwhile loop
add1
to it. Like so:Use WordPress
current_post
to check the post index in while loop.