Right now I have a very basic WordPress loop:
<?php
// get posts
$posts = get_posts(array(
'post_type' => 'page',
'posts_per_page' => 30,
'order' => 'DESC'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php the_title(); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Basically I’m trying to insert an ad between the posts.
And have an option to specify after how many posts the ad code would appear.
So if I type 3, then it would look like this:
Post 1 title
Post 2 title
Post 3 title
AD
Post 4 title
Post 5 title
Post 6 title
AD
...
In the other similar thread I have found that I can do that using a counter:
$i = 1;
if($i==3||$i==5||$i==10){
/*Adsence here*/
}
$i++;
But I have no idea how to implement that into my code.
Not a coder, just trying to combine something.
Thanks.
2
Answers
You can initialize a counter, set it to zero and add one to it after every loop. You can then check for the current value of the counter and do things when it reaches a certain value.
You could set this up in multiple ways. For example you could use
current_post
inside the loop to get the current loop index:Or if you want to use a
counter
, then you could do something like this: