how to show adsense ads in wordpress sidebar

Adding adsense is pretty easy – copy and paste the code where you want the ads to appear.

If you want a 120*600 skyscraper to appear in your sidebar at the bottom:

<script type=”text/javascript”><!–
google_ad_client = “pub-X”;
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = “120x600_as”;
google_ad_type = “text_image”;
google_ad_channel =””;
//–></script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”&gt;
</script>

would be copied to this location in the default style sidebar.php:

</ul>
</li>
<?php } ?>
<—–paste it there
</ul>
</div>

You can experiment with various places

If you want the ad right at the top of the whole page:

<body>
<—–paste the code there
<div id=”page”>

If you want the ad at the bottom of your posts:

</div>
<—–paste the code there
<?php get_sidebar(); ?>

The above is all fairly basic.
But what if you wanted an ad to only appear under the first post and nowhere else ?Open your theme index.php, and find this line:

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

Immediately above that, paste this:

<?php
$postnum = 1;
$showadsense1 = 1;
?>

That’s part one of the code – the value.

Staying in index.php, scroll down to this part:

<p>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?>  <?php comments_popup_link(‘No Comments »’, ’1 Comment »’, ‘% Comments »’); ?></p>
</div>
<—– Note this gap !
<?php endwhile; ?>

Into that gap, you paste this:

<?php if ($postnum == $showadsense1) {
echo ‘
Your adsense code goes here
‘;
} ?>

<?php $postnum++; ?>

You put ALL your code, plus the bit at either end, into the gap I pointed at.
The ‘value’ is where to show the post. The bottom part checks to see what the number is and when the number matches, it puts the ad on the screen. So if you made $showadsense1 = 3; in both locations, the ad would appear only after the 3rd ad.

Want 1 ad after the first post and 2 after the second ?
Copying from above, you would use this instead:

<?php
$postnum = 1;
$showadsense1 = 1;
$showadsense2 = 2;
?>

which would go at the top.

Again copying from the above, you already have this:

<?php if ($postnum == $showadsense1) {
echo ‘
Your adsense code goes here
‘;
} ?>

<?php $postnum++; ?>

and you would add another block below the first ad, but above the counter at the bottom. Here it is all together:

<?php if ($postnum == $showadsense1) {
echo ‘
Your adsense code goes here
‘;
} ?>

<?php if ($postnum == $showadsense2) {
echo ‘
Your adsense code goes here
‘;
} ?>
<?php $postnum++; ?>

The first bit says the values, then the values are checked each time WP goes through The Loop.
If the counter matches what your value is, the ad is shown. Changing the numbers lets you change the placement.

Lastly, maybe you want an ad ONLY when people are looking at a single post.If you are using the default theme, open single.php (or any theme that has such a file), put the adsense code right below this line:

<?php the_content(‘<p>Read the rest of this entry »</p>’); ?>

If you are not using such a theme, you need different code.
Decide where you want the ad to appear, and paste this code in that location:

<?php
if (is_single()) { echo ‘
Your adsense code goes here
‘;} ?>

You can ads anywhere and make them appear under certain conditions only. So you could ads just for pages, others in the sidebar on the home page with different sidebar sizes on archived posts, no ads at all on the main page but scattered throughout the blog on other views.
It’s the conditional if (is_single) or if (is_home) and other template tags that you need to play with.
About these ads

Posted on February 25, 2012, in seo, wordpress and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: