WordPress is a great system for newcomers merging onto the web. It provides easy-to-understand tools in a beautiful Admin backend wrapper. Along with content publication you’ll find that advertising is a big topic of discussion.

WordPress users are familiar with some of the basic trends for advertising. AdSense has always been popular, along with similar companies such as Technorati Media. In this tutorial I’d like to share a couple tricks for building customized WordPress ad blocks within your template. Development can be tricky if you are new to PHP. But it’s not awful to pickup and start running right away on your own layout.

Wordpress Automattic offices in San Francisco, California

AdSense-Ready Themes

This term is thrown around a lot when searching through WordPress themes. Basically this means any type of post or page on your website will be optimized to display a number of different Google ads.

The sizes vary greatly depending on how your layout is formatted. Some people tend to insert ads between posts, within the sidebar, along heading banners, etc. The best way to gain revenue is through clicks above the fold on your page. You want visitors to notice your ads without distracting from the main content. Below is a list containing some of the more popular ad display sizes.

  • Leaderboard – 728×90
  • Skyscraper – 120×600
  • Wide Skyscraper – 160×600
  • Banner – 468×60
  • Square – 250×250
  • Medium Rectangle – 300×250

If you’re familiar with the JavaScript code then this bit of information acts as a refresher. It is very simple to directly implement this block into your template. But instead let’s look at a different case to incorporate advertisements after every few posts.

Ads Every ‘x’ Posts

This is a common theme for custom ad displays on the homepage of any WordPress blog. When you have a list of articles it can be handy to setup an ad every 2 or 3 posts instead of directly at the top of your page. This requires a small bit of PHP code but nothing above beginner-level experience.

Inside your template folder find the index.php file which represents your home listing. We will setup a custom variable $count to check throughout the posts loop. When we hit the number we want a simple if/else clause can append the AdSense code and reset the counter if needed.

$count = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
    if ($count == 3){
      adsense code here


On a similar note we could use the PHP modulus operator to supplement an ad block every couple of posts. So instead of including ads ‘after the 3rd post’ we would display them ‘every 3 posts’ until the page content ends. This is another very popular solution especially when you have long lists of story pages.

We can still use a similar setup as above with the count variable. Yet now we can include ads every set number of posts and reset the value afterwards.

if (have_posts())
$count = 0;
  while (have_posts()) : the_post();
 if ($count%3== 0) : ?>

// adsense code here

<?php else : ?>

// no ads (regular content)

endif; endwhile; endif;
$count = 0; ?>

Targeting Individual Pages

There will also be times where you have a specific style of advertisement which needs to appear on a set number of pages. WordPress has built-in functionality for checking the type of page you’re currently viewing.

These conditional functions are the best way to modify your template without custom codes. You don’t even need an if/else clause because the conditionals return a boolean true or false value. And there are practically examples for every page in the templating system! Check out my small collection list below:

Some of these operators are trickier to use than others. And in some instances you can even select specific pages to check against. For example, you can pull a page ID using is_page( '17' ) and only display ads on that specific page. This will also work for posts and author pages, along with archive templates for categories and tags.

Advertisements in Post Content

Another such customization for your template is to display ad content right in the middle of your post. This is a bit trickier than the previous methods, but if you copy/paste the code over properly you’ll be just fine.

First you want to find the template file for editing – in this case let’s choose single.php. Then scroll until you find the line with this function:

<?php the_content(); ?>

We will be replacing this call with another block of code. It will set a variable for the paragraph number after which you want ads to display. Don’t worry too much if you don’t understand the loops, just be sure to customize the ‘ADSENSE CODE HERE’ with your own ads.

$show_after_parapgraph = 2;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $show_after_parapgraph)
    $contents = explode("</p>", $content);
    $paragraph_count = 1;
    foreach($contents as $content)
        echo $content;

        if($paragraph_count == $show_after_paragraph)
                ADSENSE CODE HERE
        echo "</p>";

What I especially like about this system is how rigorous and powerful it can be. It’s so much easier to increase your CTR through means of sprinkling advertisements within your post content. You could alternatively search through the post HTML for img tags or other modifiers to signify where you’ll place ad blocks.

Popular Plug-ins

Aside from coding these solutions manually there are plugins you can use. I don’t recommend these methods for advanced users since you gain much more control by programming them yourself. Plus not all these systems will support media from your ad company of choice.

But with that said WordPress developers are very intuitive. The system is fluid and responds well to variable content. If you have an interest in easier ad displays definitely give a few of these options a try.

  • Ad Injection – Injects any adverts (e.g. AdSense) into the WordPress posts or widget area. Restrict who sees ads by post length/age/referrer or IP. Cache compatible.
  • Smart Ads – Allows you to intelligently insert ads (like Adsense) before and after your post content based on post wordcount, post date, etc.
  • Simple Ads Manager – Advertisment rotation system with a flexible logic of displaying advertisements.
  • Quick AdSense – Quick Adsense offers a quicker & flexible way to insert Google Adsense or any Ads code into a blog post.
  • AdSense on Top – Insert Google Adsense Ads automatically to the Top, Bottom or left/right margin of your page. Adsense Optimizer Plugin required.
  • WhyDoWork AdSense Plugin – WhyDoWork Adsense allows you to insert Adsense ads on your blog without modifying the template with a variety of useful options.


I hope this tutorial can introduce some helpful coding tips for WordPress users. Being a webmaster is certainly not the easiest job in the world and does require heavy maintenance. But once you learn these techniques it becomes much easier to start earning money and drawing attention onto your website. If you have similar ideas or coding suggestions feel free to share with us in the post discussion area.

Posted by Jake Rocheleau

Jake is a writer & digital designer/illustrator. He writes about all things web and creative. Check out his website for work samples and follow his latest updates on Twitter @jakerocheleau.

Leave a reply

Your email address will not be published. Required fields are marked *