Ask your WordPress questions! Pay money and get answers fast! (more info)

Inserted Code after X number of posts WordPress

  • SOLVED

I ahve another (I think) simple question....

How would I insert HTML in between posts in a loop?

For example, I have a 15 post loop but after the 3rd post and between the 4th post, I want to insert an ad, how is that handled?

Here's my loop:



<?php
query_posts(array(
'posts_per_page' => 15
));
while (have_posts()) : the_post(); $i++; ?>
<div class="item"><a href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail('feat-bar-top'); ?>
<?php } ?>
<span class="title"><?php the_title(); ?></span>
</a></div>
<?php endwhile; ?>



What needs to be added?

Thanks!

Answers (3)

2013-11-10

Kyle answers:

You already have the counter so just use that in a conditional logic check:

<?php

query_posts(array(

'posts_per_page' => 15

));

$i = 0;

while (have_posts()) : the_post(); $i++; ?>

<div class="item"><a href="<?php the_permalink(); ?>">

<?php if (has_post_thumbnail()) { ?>

<?php the_post_thumbnail('feat-bar-top'); ?>

<?php } ?>

<span class="title"><?php the_title(); ?></span>

</a></div>

<?php if ($i == 3){ echo 'stuff here'; } ?>

<?php endwhile; ?>


Brennen Jones comments:

That did the trick!

Thanks!


Kyle comments:

You're welcome!


Brennen Jones comments:

Would I be able to add another query in the echo stuff here if I wanted to say for example, I wanted to have a different post from a different category such as a sponsored post?

Thanks!


Kyle comments:

Sure, but you should use New WP_Query instead of query_posts for that one. You may want to post another question about that if you have questions there, you will get a lot of good replies for a question like that.

2013-11-10

Remy answers:


<?php

query_posts(array(

'posts_per_page' => 15

));

$i = 0;

while (have_posts()) : the_post();
if( $i == 3 ) : ?>
// your ad
<?php endif ?>
<div class="item"><a href="<?php the_permalink(); ?>">

<?php if (has_post_thumbnail()) { ?>

<?php the_post_thumbnail('feat-bar-top'); ?>

<?php } ?>

<span class="title"><?php the_title(); ?></span>

</a></div>
<?php
$i++;
endwhile; ?>


Brennen Jones comments:

Thanks! Yours worked as well but Kyle beat you to it. :-(


Remy comments:

<blockquote>Would I be able to add another query in the echo stuff here if I wanted to say for example, I wanted to have a different post from a different category such as a sponsored post?</blockquote>

You can either do a new WP_Query or a get_post with the id of the post you want to include

2013-11-10

Abdelhadi Touil answers:

Hi.
You can do as following in this tutorial:

[[LINK href="http://techably.com/show-adsense-between-wordpress-posts/2094/"]]http://techably.com/show-adsense-between-wordpress-posts/2094/[[/LINK]]

Good luck!


Abdelhadi Touil comments:

You can also use this code:

<?php if( $wp_query->current_post == 2 ) { ?>
DO SOMETHING
<?php } ?>


Source:

[[LINK href="http://wordpress.stackexchange.com/questions/61136/insert-image-or-ad-script-after-3-posts-using-the-loop"]]http://wordpress.stackexchange.com/questions/61136/insert-image-or-ad-script-after-3-posts-using-the-loop[[/LINK]]


Brennen Jones comments:

Thanks for your suggestion but the other two gents beat you to it I'm afraid.


Abdelhadi Touil comments:

No problem. What is important is that you get the solution :)
Good luck!