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

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:
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

Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...