Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.
Notice: I'm taking a break from blogging to focus on Atari Gamer . com, check it out!

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
When I started modifying the FlatPress theme I am using to include AdSense content, I wanted to be able to have the main blog page not displaying any ads within the entry, but when viewing a single entry, I wanted additional ads shown inline with the entry itself.

Here's an example:

By taking the naive approach and just adding the AdSense code to the entry-default.tpl file will violate the Google policy for placing ads. The policy is quite strict and has limits on how many ad units are permitted per page. Without additional code in the template, the AdSense content will get repeated over and over.

The rules for displaying or not displaying the AdSense code are like this:
  • Do not show on the main blog page
  • Do not show on the categories list
  • Do not show on any other blog page listings (page 2, page 3, etc)
  • Show on the entry page

With the above rules in mind, I wrote the following code:
if (substr_count($_SERVER[REQUEST_URI], "index.php/") > 0 AND
substr_count($_SERVER[REQUEST_URI], "/category") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/page") == 0) {
<div class="adsenseentry">

Instead of the ellipsis (...), I have my actual AdSense code from Google (omitted here).

This code assumes that the pretty URLs plugin is used and the .htaccess is set up correctly. It will not display the AdSense content for category and page listings, but only for the entry page itself.


Skip down to comments...
A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. If you spot something out of place, please do let me know.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...