Top

Adding Banners after posts in WP

June 9, 2008 by admin 

From Shem’s Business forum at Medium Pimpin

NY Jester had asked about my blog Bountiful Babes and how I got banners to show up in the posts so I thought i’d create a new post so everyone can find out if they’re interested.

The way I have those banners show up is that they’re “hard coded” if you will, on to my wordpress templates so the same banners will show up on every page, because of this I pick my 7 favorite sponsors (since I have 7 posts showing on each page) and save a banner for each one that I feel my surfers would be attracted to. Now my banners don’t get as much traffic as my blog posts/pictures but they still get me traffic so I make use of the space. Now I have the banners I want to use after the posts I go in to my wordpress templates.

This “coding” will work for any wordpress template and is also useful in mainstream blogs as well (I’ll give an example later if you’re interested). Now for the code, you want to open your index.php template page from your wordpress backend.

Find where it says
<?php while (have_posts()) : the_post(); ?>

replace that line with
<?php while (have_posts()) : the_post(); $loopcounter++; ?>

This enables a “counter” so you can “count” posts on the page, ie: post 1,2,3,4,5,6,7 (since I have 7 posts per page)

Now that you’ve numbered the posts you want to tell your blog what to post after each post and where.

Find the section of your index.php where you want the banner to show, in the example of Bountiful babes I went to the end of the post code:

<div class=”storycontent”>
<?php the_content(__(‘(more…)’)); ?>
</div>
<?php
else:
echo “”;
endif; ?>

(This code is going to be different for each template, this is simply how it’s laid out on my template) and then right after that section of code I place this code:

<?php if ($loopcounter == 1) { ?>

<div class=“adsense”><b><center><a href=”LINK1″><img src=”BANNER1″></a></center></b><br></div>
<?php } ?>

<?php if ($loopcounter == 2) { ?>

<div class=“adsense”><b><center><a href=”LINK2″><img src=”BANNER2″></a></center><br></b></div>
<?php } ?>

<?php if ($loopcounter == 3) { ?>

<div class=“adsense”><b><center><a href=”LINK3″><img src=”BANNER3″></a></center><br></b></div>
<?php } ?>

<?php if ($loopcounter == 4) { ?>

<div class=“adsense”><b><center><a href=”LINK4″><img src=”BANNER4″></a></center></b><br></div>
<?php } ?>

<?php if ($loopcounter == 5) { ?>

<div class=“adsense”><b><center><a href=”LINK5″><img src=”BANNER5″></a></center></b><br></div>
<?php } ?>

<?php if ($loopcounter == 6) { ?>

<div class=“adsense”><b><center><a href=”LINK6″><img src=”BANNER6″></a></center></b><br></div>
<?php } ?>

<?php if ($loopcounter == 7) { ?>

<div class=“adsense”><b><center><a href=”LINK7″><img src=”BANNER7″></a></center></b><br></div>
<?php } ?>

This code tells your blog what image to pull from where and what to link it to (just make sure you replace the LINK and BANNER sections of code above with the right links!). For those concerned about not having an “adsense” category not coded in to your css sheet it should pull up just fine anyway but if you’d prefer you can add one yourself.

Now you should be getting banners pulling up after each of your posts, just change the <center> alignment to left/right if you’d prefer different alignment of your banners.

I mentioned mainstream blogs above as well which brings to mind one of my mainstream blogs that I wanted to display google adsense after every other post instead of every post. You can do this with the same process above but instead of the second section reading as so:

<?php if ($loopcounter == 1) { ?>

<div class=“adsense”><b><center><a href=”LINK1″><img src=”BANNER1″></a></center></b><br></div>
<?php } ?>

You want to place this code instead:

<?php if ($loopcounter % 2 == 0) { ?>

<div class=“adsense”>ADSENSE SCRIPT GOES HERE</div>

<?php } ?>

This tells the counter to only place google ads after every second post, this is especially helpful when you have more than three posts per page since Adsense has restrictions on how many ads you can show per page and it gives a more uniform look to your ad spots on your blog. If you want every 3 posts change the 2 above to a 3 and so on.

Hope that helps some of you placing ads on your blogs

Got question about this tip?

Comments

Got something to say?





Bottom