
Contents In This Article
How to show other content (such as an ad) after every “X” number of posts
Some Questions
- inserting ads between every 7th post of homepage and category WordPress page
- Inserting ads after every 7th post in WordPress home page
- I have WordPress website and I want to implement Adsense ads into it.
- instert adsense ad after x number of posts
- PHP if foreach show ads every 5 loop
Inserting ads between every 7th post of homepage and category wordpress page
I am trying to insert google ads between every 7th posts in my wordpress site with infinite scroll. i searched and found results but that didn’t worked for me because the WordPress theme i am using is little complex. the result i found was to add this code
<?php $postnum++; if($postnum%5 == 0) { ?>
YOUR AD CODE HERE
<?php } ?>
after <?php endwhile; ?>
in index.php, but in my case the loop is different from the general wordpress theme. the loop i have is like this and i want to know how can i implement the above code in my case.
Answer :
Try this in front-page.php. You may have to adjust it to add after correct number of posts.
if( $wp_query -> post_count > 0 ){ $postnum = 0; foreach( $wp_query -> posts as $post ){ $postnum++; if( $postnum%5 == 0 ) { echo '
—- insert ad here —-
‘; } $wp_query -> the_post(); $post_id = $post -> ID; ?>
it should replace this bit of code
if( $wp_query -> post_count > 0 ){ foreach( $wp_query -> posts as $post ){ $wp_query -> the_post(); $post_id = $post -> ID; ?>
Inserting ads after every 7th post in wordpress home page
What you can do is to create a $counter then to display based on it the adcode. So the final code for you will be this one:
$counter = 1; if(have_posts()) : while(have_posts()) : the_post(); ?> } if ( floatval(get_bloginfo('version')) < "3.6" ) { //old post formats before they got built into the core get_template_part( 'includes/post-templates-pre-3-6/entry', get_post_format() ); } else { //WP 3.6+ post formats get_template_part( 'includes/post-templates/entry', get_post_format() ); } ?-->
[adcode]
We just increment the $counter, and display the [adcode] after each seven posts.
100% Work
<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>
1 Comment