Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

| Views: 5367
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.


Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
comments powered by Disqus
Other posts you may like...