I have Wordpress website and I want to implement Adsense ads into it.
I have 30 posts per page so i want to show ads after every 7 posts, how can I do this? Currently I am using this method for 3 ads in 10 posts, and after 10 posts no ads showing:
<center><?php if( $wp_query->current_post == 1 ) : ?>
Adsense Code Here
<?php elseif( $wp_query->current_post == 3 ) : ?>
Adsense Code Here
<?php elseif( $wp_query->current_post == 7 ) : ?>
Adsense Code Here
<?php endif; ?></center>
I want to show ads after every 7 posts, is that possible in one code line ?
You need to use the modulus (or "mod") operator
%
to get the remainder of valuex
divided by valuey
i.e.x % y = remainder
. e.g.4 % 3 = 1
because 4 divided by 3 gives a remainder of 1.Your code should be:
How this works:
You want to display the ad after every 7 posts, so you need to use 3 as the
y
i.e. the value to divide by. This will give the results:As you want to start after the first ad, then you want to check for a remainder value of 1.
Tip:
Off topic, but the
<center>
HTML tag has been deprecated so you shouldn't use it any more. use the CSS styletext-align:center
on the container element instead.You just need to put one condition here :-
By this you will get 0 as reminder after every post count which is multiple of 7 like 7, 14, 21 etc.