PHP :WORDPRESS : Place banner between posts
I thought it would be nice to share you a little piece of script that I use on this website, with this modification in my theme I can show banners where i want them to be, I’ve decided to do this to keep some load off the sidebox, and some banners between posts are real eye catchers! Applying these changes requires basic programming skills and should not take any longer than 10 minutes.
Step 1
First or all you have to determine what theme you are running,to do this click on the presentation tab, in the administration panel.
now you can see your current theme and the locations of the files relative to your blog.
Step 2
Connect to your website with your favorite FTP Browser and go to the folder we have found in step one, now we have to find a file that handles the posts, this can vary from theme to theme, in default theme this file is called page.php and it looks like this.
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Step 3
All we have to do know is add the modifications and define the ads/banners that have to be shown and where they have to be shown. here is the modified code.
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
// change part 1
// we have to set this value to keep track what the number of the current post is.
$count_post = 1;
// end -> change part 1
if (have_posts()) : while (have_posts()) : the_post();
?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php
// Change part 2
// after the first post
if ($count_post == 1) {
$buff="<div style=\"margin:5px; padding:5px;\"><center>".adsensem_ad('ad-468')."</center></div>";
echo $buff;
}
//end -> after the first post
// after the third || fifth || seventh post
if ($count_post == 3 || $count_post == 5 ||$count_post == 7) {
$buff="<div style=\"margin:5px; padding:5px;\"><center><script type=\"text/javascript\" src=\"\"></script></center></div>";
echo $buff;
}
//end ->after the third || fifth || seventh post
// set the new track number (old value +1)
$count_post++;
// end -> Change part 2
?>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
As you can see I’ve added $count_post this value will keep track of the post numbers.
before endwhile; is where all the action takes place, In this case the are 2 different ads, the first one will be shown only after the first post , the second howeven will be shown after the 3th, 5th and 7th post, ofcourse you can change these values to whatever you want.
In my case the first ad is managed with Adsense Manager The second advertisement is using an javascript banner randomizer offered by many affilate programs ,like NeverBluAds
if you plan to use this, or if there is something not clear, or need help applying this code, leave a comment and I’ll try to help you.
Tags: hack, wordpress, wordpress modification
























March 15th, 2008 at 8:41 am
Thanks for sharing that. Good way to customise the ad and banner units that appear between posts.
Andrew’s last blog post..Productivity tips revisited
March 15th, 2008 at 3:56 pm
I’ve been looking for that for my other blog. Thanks for sharing!
Crystal’s last blog post..12 Awesome Blog Category Ideas And Others