How to Insert Multiple AdSense Inside Post?
Recently, I’ve been asked by my friend who has a Wordpress blog with very long post at each page. He wants to know how to insert multiple AdSense ads inside a post automatically. It seems that he want to locate all of his AdSense ads on many paragraphs, as provided in the sample below:
Post (1st paragraph)
Adsense / other advertisements
Post (2nd paragraphs – 3rd paragraphs)
Adsense / other advertisements
Post (4th paragraphs – 5th paragraphs)
Adsense / other advertisements
Post (6th paragraphs – 9th paragraphs)
For implementing AdSense or other advertisements between any paragraphs, we just need one counter variable to save the post/paragraph index (It will be valued 1 till maximum paragraph you have on a page). Then we can use that variable to determine the post automatically and will display AdSense or other advertisements in the place where we wanted to be displayed.
So, let’s move forward and take a look at the sample for the code below:
(This modified code is using Default Theme – Main Index Template)
<?php if (have_posts()) : ?>
<?php $ctr = 1; ?>
<?php while (have_posts()) : the_post(); ?>
<div class=”post” 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>
<small><?php the_time(’F jS, Y’) ?> <!– by <?php the_author() ?> –></small>
<?php if ($ctr == 1) { ?>
//AdSense or other advertisements here
<?php } ?>
<div class=”entry”>
<?php the_content(’Read the rest of this entry »’); ?>
</div>
<p class=”postmetadata”>Posted in <?php the_category(’, ‘) ?> | <?php edit_post_link(’Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(’No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
</div>
<?php $ctr++; ?>
<?php endwhile; ?>
Explanation:
<?php $ctr = 1; ?>
At the 3rd line, we prepare the $ctr variable which will be used for counter as already mentioned above. The value is “1”, since the first paragraph will be the first post.
<?php if ($ctr == 1) { ?>
// AdSense or other advertisements
<?php } ?>
The above codes is used for checking the counter, if the value of counter is “1” then AdSense or other advertisements will be displayed at “// AdSense or other advertisements”. Just change that text with your AdSense codes, or you can insert any contents you like on that area.
The first counter value (“<?php $ctr = 1; ?>”) can be modified with any number depends on your choice, where is the first location for your AdSense will be displayed. If you want insert your first AdSense inside the 3rd paragraph, just change the above code with “<?php if ($ctr == 3) { ?>“
If you want to locate AdSense at different places, you can use ( || ), the OR operator function from PHP.
Watch this sample code; it will insert AdSense ads on 1st location and 3rd location:
<?php if ($ctr == 1 || $ctr == 3) { ?>
But, if the inserted advertisement codes have different location, you need to write the condition one-by-one. Watch this sample:
<?php if ($ctr == 1) { ?>
// Google AdSense advertisement
<?php } ?>
<?php if ($ctr == 3) { ?>
// Oxado Advertisement
<?php } ?>
At last, to make incremental counter variable, use <?php $ctr++; ?> code, so the post/paragraph index will be appropriated.
If you are using this codes (“<?php $ctr++; ?>”) for other themes, please make sure that this code is located exactly above the <?php endwhile; ?>.
This trick can be used for other Wordpress Templates such as Main Index, Archive, Search Result, and Tags (If you’re using Ultimate Tags Warrior).
Tags: div class, index template, default theme, adsense, paragraphs, h2, lt, advertisements, js, paragraph, wordpress blog
Wednesday, June 20th, 2007
Related Post:
How to Insert AdSense Codes Inside Post ?
Search Engine Optimization
Wrap Adsense Inside Post
What Actually is Google AdSense Channel ?
How to Make Submit Links to Social Bookmarks?