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:
Create a function that will display your ads and enter it after the variable initialization described above. Our example does it as follows:
}
Then add a set of IF statements that utilize the counter to display your ads in sequence:
if (nPostCounter == 1) {
}
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:
nPostCounter++;
nPostCounter++;
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:
Just before the end tag for this <p> section, paste the following:
rotatePostAd();
</script>
That's all there is to it!