Getting started with the OpenLinkz search widget - read these posts and get up and running

Setting up OpenLinkz OpenLinkz Simplified Changing Appearance of Links OpenLinkz Cheat Sheet

How to set up ads after each post in Blogger

For those of you who use Blogger as your blogging platform, you may be wondering how to set up ads after each post.

Blogger allows users to set up AdSense ads after each post. But what if you want to use ads from an affiliate network or ad network of your own choice? And especially, how do you display a different ad after each post when there are multiple posts on the page? Blogger doesn't have a built-in page element that supports that functionality.

This post explains how to do it. If you know a little Javascript, it won't be too hard for you to implement.

First, you need to decide how many posts you want to display on the homepage of your blog. For our example, let's say three posts will be displayed.

We then need to set up a counter that keeps track of which post is being displayed. Blogger will loop through posts until it hits the maximum number that you designated.

Add an HTML/Javascript page element to your blog. Add some script tags to it and implement the code for the counter, initializing it as follows:

var nPostCounter = 1

Create a function that will display your ads and enter it after the variable initialization described above. Our example does it as follows:

function rotatePostAd () {
}

Then add a set of IF statements that utilize the counter to display your ads in sequence:

function rotatePostAd () {

if (nPostCounter == 1) {

nPostCounter++;
} else if (nPostCounter == 2) {

nPostCounter++;
} else if (nPostCounter == 3) {

nPostCounter++;
}

}

Note that the code increments the counter each time a post is displayed on the page.

Then, we need to obtain the HTML or script for the three ads that will be displayed. The trick is to take the statements that cause the ad to be displayed and convert those statements to Javascript document.writeln statements. This is not as hard as it seems. Peter Bromberg has written an article at EggheadCafe titled "Convert HTML/Script to Javascript Include Source". The article includes a converter. Paste the code for an ad into the first text box, click the Convert button and retrieve the document.writeln statement from from the second text box. Then take the document.writeln statement and plug into our function. After doing this three times, our function will look like the following:

function adRotator () {
if (nPostCounter == 1) {
document.writeln('converted code for first ad... ')
nPostCounter++;
} else if (nPostCounter == 2) {
document.writeln('converted code for second ad... ')
nPostCounter++;
} else if (nPostCounter == 3) {
document.writeln('converted code for third ad... ')
nPostCounter++;
}

}

We now have a function that will display a different ad for each post. We now have to call the function from the appropriate place in the Blogger template. Go to the Layout tab in Blogger and click "Edit HTML". Click the "Expand Widget Templates" checkbox. Scroll down to the section that displays the labels for a post. Look for the following line of code:

<p class='post-footer-line post-footer-line-2'>

Just before the end tag for this <p> section, paste the following:

<script type="'text/javascript'">
rotatePostAd();
</script>

That's all there is to it!

Posted by TradeRadarOperator at 8/02/2008 09:36:00 PM  

0 comments:

Post a Comment

Digg!



 
Support our sponsors!
Close this ad panelX