how to make ADSENSE appear every 4 posts on wordpress?

Status
Not open for further replies.


$count++;
if $count==4 {insert ad; $count=0}

drop it in the loop right after the post.
 
in index.php:

1
before the line
Code:
if(have_posts() : ...
add:
Code:
<?php $postnum = 1; ?>
2
after the block
Code:
<div class="entry"> ... </entry>
add:
Code:
<?php if(($postnum % 4) == 0) : ?>
<!--adsense code here-->
<?php endif;
$postnum++; ?>
Oh yeah.
 
in index.php:

1
before the line
Code:
if(have_posts() : ...
add:
Code:
<?php $postnum = 1; ?>
2
after the block
Code:
<div class="entry"> ... </entry>
add:
Code:
<?php if(($postnum % 4) == 0) : ?>
<!--adsense code here-->
<?php endif;
$postnum++; ?>
Oh yeah.

Thanks for the code, i implemented it in index.php, but now its showing after post 1 and after post 4. How do i do it so that it shows after post 4?
 
Just keep messing with the php. Also remember you can't have more than 3 ad blocks, on search and one link block.

Google "wordpress adsense in between posts" as well. I've done this before, but am on a blackberry so my abilities are limited.

Also, one after the first or second post is good. One at the top and 160x600 in left sidebar has worked well for me. That will be three above the fold if you do it right.
 
Code:
<?php get_header(); ?>
<div id="column">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- ^^^^^^^^^^^^^^ post code ^^^^^^^^^^^^^^ -->
<div class="post" id="post-<?php the_ID(); ?>">
<div class="post-top"></div>
<div class="entry">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<span class="date"><?php the_time('l, F j, Y'); ?> <?php the_time('G:i'); ?></span>
<div class="info">
<div class="cat">Posted in category <?php the_category(', ') ?></div>
<div class="comments"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
<div class="clear"></div>
</div>
<div class="content">
<?php the_content('Read the rest of this entry »'); ?><div class="clear"></div>
</div>
<div class="tags"><?php edit_post_link('Edit', '', ' | '); ?><?php the_tags('Tags: ', ', ', ''); ?></div>
</div>
<div class="post-bottom"></div>
</div>
<!--adsense code here-->
<?php if(($postnum % 4) == 0) : ?>
<?php endif;
$postnum++; ?>
<!--adsense code here-->
<!-- ^^^^^^^^^^^^^^ post code ^^^^^^^^^^^^^^ -->
<?php endwhile; ?>
<!-- ^^^^^^^^^^^^^^ post navigation ^^^^^^^^^^^^^^ -->
<div class="navigation">
<!-- Start Of Script Generated By WP-PageNavi 2.20 -->
<center><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></center>
<!-- End Of Script Generated By WP-PageNavi 2.20 -->
</div>
<!-- ^^^^^^^^^^^^^^ post navigation ^^^^^^^^^^^^^^ -->
<?php else : ?>
<!-- ^^^^^^^^^^^^^^ page not found + search form ^^^^^^^^^^^^^^ -->
<div class="post">
<div class="post-top"></div>
<div class="entry">
<div class="content">
<h1>Page Not Found</h1>
<p>Sorry, but you are looking for something that isn't here.</p>
<h3>Search Blog</h3>
<?php include(TEMPLATEPATH."/searchform.php"); ?>
</div>
</div>
<div class="post-bottom"></div>
</div>
<!-- ^^^^^^^^^^^^^^ page not found + search form ^^^^^^^^^^^^^^ -->
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
 
It looks a bit chaotic, but I think you get the picture. Add or remove anything between ##### and ===== as noted.

You basically didn't set $postnum to 1 before the WP loop starts. You also have 2x the adsense code in there.

Code:
<?php get_header(); ?>
<div id="column">
<!-- ################# ADD ############### -->
<?php $postnum = 1; ?>
<!-- ======================================== -->
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- ^^^^^^^^^^^^^^ post code ^^^^^^^^^^^^^^ -->
<div class="post" id="post-<?php the_ID(); ?>">
<div class="post-top"></div>
<div class="entry">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<span class="date"><?php the_time('l, F j, Y'); ?> <?php the_time('G:i'); ?></span>
<div class="info">
<div class="cat">Posted in category <?php the_category(', ') ?></div>
<div class="comments"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
<div class="clear"></div>
</div>
<div class="content">
<?php the_content('Read the rest of this entry »'); ?><div class="clear"></div>
</div>
<div class="tags"><?php edit_post_link('Edit', '', ' | '); ?><?php the_tags('Tags: ', ', ', ''); ?></div>
</div>
<div class="post-bottom"></div>
</div>
<!-- ############# REMOVE: ################### -->
<!--adsense code here-->
<!-- ============================================ -->
<?php if(($postnum % 4) == 0) : ?>
<!-- ############### ADD: #################### -->
<!-- adsense code here -->
<!-- ============================================== -->
<?php endif;
$postnum++; ?>
<!-- ############## REMOVE: ################## -->
<!--adsense code here-->
<!-- ================================================ -->
<!-- ^^^^^^^^^^^^^^ post code ^^^^^^^^^^^^^^ -->
<?php endwhile; ?>
<!-- ^^^^^^^^^^^^^^ post navigation ^^^^^^^^^^^^^^ -->
<div class="navigation">
<!-- Start Of Script Generated By WP-PageNavi 2.20 -->
<center><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></center>
<!-- End Of Script Generated By WP-PageNavi 2.20 -->
</div>
<!-- ^^^^^^^^^^^^^^ post navigation ^^^^^^^^^^^^^^ -->
<?php else : ?>
<!-- ^^^^^^^^^^^^^^ page not found + search form ^^^^^^^^^^^^^^ -->
<div class="post">
<div class="post-top"></div>
<div class="entry">
<div class="content">
<h1>Page Not Found</h1>
<p>Sorry, but you are looking for something that isn't here.</p>
<h3>Search Blog</h3>
<?php include(TEMPLATEPATH."/searchform.php"); ?>
</div>
</div>
<div class="post-bottom"></div>
</div>
<!-- ^^^^^^^^^^^^^^ page not found + search form ^^^^^^^^^^^^^^ -->
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
 
Status
Not open for further replies.