Home

Add Banners or Google Adsense to Your Blog

by David Pankhurst

Wondering how I display my banner advertising here on my blog? The answer is quite simple, actually, just a bit of tinkering with the source code…

There’s a number of ways to tuck ads into a blog – for WordPress especially, it’s straightforward to place an ad before (or after) every entry.

In WordPress, the index.php file does all the text output, so changes can be localized there. The area you want looks somewhat like this:

<div id="content">
<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
...
<?php endforeach; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>

There’s more, but you’re looking at the main display loop in WordPress – the ‘foreach’ statement starts a loop for each entry displayed (‘for each’ – get it?). Here is where you’d place an ad, and have it display just before the text.

As another option, you can display the ad just after the article, by placing the html just before this statement:

<?php endforeach; else: ?>

An alternative is to display ads when nothing is found (invalid search, for example). In that case, the ad code can be placed here:

<?php endforeach; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
...put your html here
<?php endif; ?>

Finally, you can display after all entries end (one time only) by placing code between these two lines:

<?php endif; ?>
...code here...
</div>

Of course, Google AdSense only allow three ads per page, so if you have to make sure you don’t display more than three at a time, or program a limit with a counter.

This just opens some options. If you have code that can display your banners, then you’ll likely want them displayed as often as reasonable. Using WordPress, you can add multiple banners simply and easily – or any other code you want repeated.

Leave a Reply