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

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:
adsense_fp1.png
adsense_fp2.png


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:
{php}
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) {
{/php}
<div class="adsenseentry">
...
</div>
{php}
}
{/php}


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.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

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.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.