Inserting a widget in-between posts Published on February 25, 2010
Sometimes you need to insert advertising banners, images, widgets or any sort of “gizmo” between you posts, archives or category listings.
Let’s say you need to add a flash banner between the 1st and the 2nd posts, or insert a currency calculator after the 3rd post of your page. Anything goes as long as it goes inside a widget.
To do so you’ll need your theme to be custom coded. This tutorial is about achieving that.
Step 1. – Registering the widget Area
First you need to search and edit your functions.php file in order to register the new widget area you’ll be using to place your widgets with your stuff.
This is roughly an example of what you will be looking for in your functions.php:
Find the array bellow (syntax may vary) and add your new widget area to it
if ( function_exists(‘register_sidebar’) )
register_sidebar( array(
‘name’ => ‘ wdgtSpot1′,
‘id’ => ‘wdgtSpot1′,
‘before_widget’ => ‘<div id=”%1$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h3 class=”widget-title”>’,
‘after_title’ => ‘</h3>’
) );
register_sidebar( array(
‘name’ => ‘wdgtSpot2′,
‘id’ => wdgtSpot2,
‘before_widget’ => ‘<div id=”%1$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h3 class=”widget-title”>’,
‘after_title’ => ‘</h3>’
) );
/* Add this chunk of code to the existing array, to register your new widget area */
register_sidebar( array(
‘name’ => ‘wdgtInsert’,
‘id’ => ‘wdgtInsert’,
‘before_widget’ => ”,
‘after_widget’ => ”,
‘before_title’ => ‘<div>’,
‘after_title’ => ‘</div>’,) );
/* End inserted widget area – PLS Remove these comments once finished, since they are for demonstrative purposes only */
));
Step 2. – Adding your conditions to the loop
Now you’ll need to code your theme per-se.
Find and edit your theme’s index.php to insert the widget in your main listing.
However if you want your widget to show up in between your archive listings, such as on category or tag pages, you need to edit archive.php, category.php and tag.php. – You can actually control which widget will be displayed on which pages by using WP conditional tags: is_archive(), is_category() and is_tag(), etc.
Regardless, in the chosen file to edit (in this case index.php), search for the “endwhile;” loop and paste this chunk of code, in the line directly above it (right before: endwhile or <?php endwhile; ?>)
<?php
if ($count==2) {
dynamic_sidebar(‘wdgtInsert’); // or whatever you wish to add between your posts listing
}
$count = $count + 1;
?>
This will insert your widget after the second post in your main listing.
You can configure it to insert the widget in any desired post location, by simply assigning a new value to the $count variable. Like this: $count==3 or $count==4
Remember this is telling the cycle after wich post your widget will be inserted.
Have fun!

