Display AdSense in certain Posts in WordPress

James emailed me on how I display my adsense in certain posts. As you can see on my homepage, there is an adsense above the content in my first post. And after the first post, there is another post that has adsense but without content. To do it, you just need to put a counter. We create the counter by using PHP.

First we need to declare the variables that we want to use.

$postnum – This variable use for count how many posts in the loop
$showads# – This variable is use the adsense that we want to display.

Note: Replace the # with number, eg: 1, 2, etc.

Ok here is the trick. Put this PHP code before the loop.

<?php
$postnum = 1; //Define the initial value
$showads1 = 1; //This mean it will be display in post #1
$showads3 = 3; //This mean it will be display in post #3
?>

So your WordPress index.php file will look like this:

<?php
$postnum = 1; //Define the initial value
$showads1 = 1; //This mean it will be display in post #1
$showads3 = 3; //This mean it will be display in post #3
?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Next what we want to do is to put the Adsense code in our post. Here is how to do it. Just put this if function before the content tag.

<?php if ($postnum == $showads1) { ?>

... your adsense code goes here ...

<?php } ?>

<?php the_content(__('Read more...')); ?>

And finally we increment the counter value with this code before the endwhile tag.

<?php $postnum++; ?>

Your index.php will look like this:

<?php $postnum++; ?>
<?php endwhile; ?>

After you put all the code above, your WordPress index file might look like this one.

<?php
$postnum = 1; //Define the initial value
$showads1 = 1; //This mean it will be display in post #1
$showads3 = 3; //This mean it will be display in post #3
?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="entrytitle" id="post-<?php the_ID(); ?>"><h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2></div>
<div class="entry">

<?php if ($postnum == $showads1) { ?>

... your adsense code goes here ...
<?php } ?>

<?php the_content(__('Read more...')); ?>

</div>

<?php $postnum++; ?>
<?php endwhile; ?>

If you want to only display the ads without the content, just use the if function. Ok here is the example. Let say we want to display the ads after the third post.

<?php if ($postnum == $showads1) { ?>
<div class="entrytitle"><h2><a href="#" rel="bookmark" title="Permanent Link to #">AdSense</a></h2></div>
<div class="entry">

... your adsense code goes here ...

</div>

<?php } ?>

But make sure you put the function in the loop or it might not work. I hope it is clear. If you have any problems regarding this, feel free to give comments or email me. Thanks for your time. 🙂

PS: I hope this entry helps, James. :d [tags]adsense, tips, php[/tags]

Comments

  1. Wow, simply brilliant man… you certainly know you PHP (I dont).. I’ve gotta spend some time to digest this.

  2. Well James, you can make use of Moosecandy plugin to get the job do :d

  3. well it is just simple php coding. :d

  4. thanx for the info…baru nak tanya camne ngko wat..\:d/

  5. he3. senang je. tak susah mana pun. :d

  6. Thanks for the info. I’ll use it in my new blog 🙂