PHP :WORDPRESS : Place banner between posts

WordpressI 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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Live
  • Ma.gnolia
  • NewsVine
  • StumbleUpon
  • Technorati
  • TwitThis
  • Chawlk
  • Propeller
  • Ycombinator
Tags: , ,

13 Responses to “PHP :WORDPRESS : Place banner between posts”

  1. Andrew Says:

    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

  2. Crystal Says:

    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

  3. Mark88 Says:

    You might be able to decipher the nuts and bolts, but in the end, you can’t unscramble the mystery of how music makes you feel. ,

  4. Narek Rosenberg Says:

    High and dry

  5. Pinoy Tech Blogger Says:

    blog did exactly what you said it does. Your site is great to work with. I can’t say enough about blog.

  6. AngNetwork Says:

    Download and listen Free songs - http://www.angnetwork.com/songcity/

  7. JohnQ Says:

    Why sulf the web? come and join us and you will find everything you can imagine from

  8. AudioProNews Says:

    This is nice posting from you … Congrats

  9. Ana Maria Says:

    nice article and also nice all the site … congrats and keep up the good posting :)

  10. cirebon Business Says:

    Cirebon Business Advertisement. Iklan Bisnis Cirebon adalah Direktori Usaha di kota Cirebon dan sekitarnya

  11. Dima Says:

    Ìû ( txtpromotion.ru ) ïðåäîñòîâëÿåì òîëüêî êà÷åñòâåííûå óñëóãè (êîïèðàéò, ðåðàéò, ïåðåâîäû) âñå äëÿ ïðîäâèæåíèÿ âàøèõ ñàéòîâ â òîï10

  12. Vegetarian Recipes Says:

    Great post, thanks for sharing very informative :)
    Vegetarian Recipes’s last blog post..Delicious Veggie Omelet

  13. Ciupanezul Says:

    Thanks for sharing this informative and great post.

    Ciupanezul’s last blog post..PreÅŸedinte pentru o zi

Leave a Reply