Embedding Ads Between Excerpts
One other modification you may want to make to your theme files is the ability to embed ads between excerpts. For example, let's say our index page displays ten excerpts per page, and we want to present an ad between every fourth excerpt. According to our layout sketch, this zone is identified as 3.
Note: Do not confuse embedding ads between excerpts with embedding ads between paragraphs inside an article. You can read Embedding Ads Within Posts for more information.
To enable embedding ads, open the index.php file and locate the following line(s) of code. Depending upon your theme, your code may look slightly different. Regardless, the following code elements will be present.
<?php if (have_posts()) : ?> if (while (have_posts()) : the_post(); ?>
What we need to do is insert and initalize a counter variable immediately after the have_post() function and before the while statement, like so.
<?php if (have_posts()) : $adCounter = 0; ?> if (while (have_posts()) : the_post(); ?>
Before we ad our function call, we need to test whether our ad counter is equal to the desired value. If it is only then do we execute our function call. Otherwise, we skip over the function and allow WordPress to continue processing the excerpts. This code must be placed immediately before the endwhile statement. Here is what that looks like.
<?php
$adCounter++;
if ($adCounter == 4) {
if (function_exists('cd_ad_zone')) {
cd_ad_zone(3);
$adCounter = 0;
}
}
?>
<?php endwhile; ?>
Each time WordPress processes an excerpt, the counter is incremented by one. When and if the counter value is equal to 4, we execute our function call for zone 3. If we did execute our function call, we need to reset the ad counter so that it starts over from zero. You can change the value of the ad counter to some value other than 4. The value simply means that a qualified Sponsor Ad may be placed between every nth excerpt.
If you want Sponsor Ads displayed in this manner for other lists, such as Search Results, Category Lists, etc., then you should repeat this process for each of the desired template files. You can read more about The Template File Hierarchy at the WordPress Codex web site.

Email