Exclude Adsense From Specific WordPress Posts

For one reason or another, you may decided that you don’t want Google Adsense ads appearing within certain posts on your WordPress blog. It’s a difficult task since you most likely added the Adsense ad code within the template file.

Using PHP and with just a slight change to your code, you can exclude Adsense ads from specific posts.

Let’s say that I want to exclude Adsense ads from 3 posts:

  • How to Enjoy Your Vacation
  • The Best Android Apps
  • WordPress For Dummies

I would exclude each one by using the slug for each post with this code
<?php
if (!is_single(array('how-to-enjoy-your-vacation','the-best-android-apps','wordpress-for-dummies')))
{
?>
Your Adsense code
<?php
}
?>

You can also exclude posts via the post ID.
 if (!is_single(array('133','145','200')))  
{
?>
Your Adsense code
<?php
}
?>

Published by Mitch Bartlett

I've been doing things on the Internet since 1994. Former dot-com era IT guy. Currently I provide technical solutions to people for fun and test software for a growing software company.

Join the Conversation

1 Comment

Leave a comment

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