Regulate Google Adsense Display Using WordPress Conditional Tag

Am not a fan of long articles, so am going to make this post on how to display adsense or affiliate link on specified location when certain condition is true or false using wordpress conditional tag and PHP conditional statement short and comprehensible as possible.
Have you found yourself a situation where you will like to display your adsense or any AD in a desire location but couldn’t figure it out.
Take for example: i do use a plugin called AD injection to display Adsense to my blog, with the plugin, you can place adsense or affiliate link in vary positions as specified in the plugin.
There came a time when i decided to display an adsense leaderboard unit at the top of my blog but i wanted the leaderboard adsense to display everywhere except on post page(is_single) i.e when a post is being viewed. i overcame this problem using wordpress conditional tag combine with PHP conditional statement.

We are going to make use of the three below conditional tag in this tutorial :-

  • is_home() – When the main blog page(homepage) is being display.
  • is_single() – When a single post (either attachment or custom post) page is being display. This is false for pages.
  • is_page() – When a page is being display.

For full reference about wordpress conditional tag, click here.
Note: you must have a knowledge of PHP conditional statement to be able to practise this tutorial.

Practical Examples

Use the examples below to have a basic knowledge about how this work.

  1. Display Adsense only on Homepage
    
    < ?php
    if (is_home())
      { 
        echo ' ADSENSE/AFFILIATE CODE TO DISPLAY AT HOMEPAGE HERE ';
      }
      ?>
    
  2. Display Adsense only on post (when a post is viewed)
    
    < ?php
    if (is_single())
      { 
        echo ' ADSENSE/AFFILIATE CODE TO DISPLAY ON POST HERE ';
      }
      ?>
    
  3. Display Adsense Everywhere except on Homepage
    
    < ?php
    if (is_home())
      { 
        echo ' ALTERNATIVE TO ADSENSE CODE HERE';
      }
      else 
       { 
      echo ' ADSENSE CODE HERE ';
      }
     ?>
    
  4. Display Adsense On custom post and pages only
    
    < ?php
    if (is_single())
      { 
        echo ' ADSENSE CODE TO DISPLAY ON POST HERE';
      }
      else if 
     (is_page())
       { 
      echo ' ADSENSE CODE HERE TO DISPLAY ON PAGE';
      }
     ?>
    

Conclusion
The aim of this tutorial is to give an insight on how to use wordpress conditional tag and PHP conditional statement. Sturdy the above examples and practise on your own. Don’t forget to learn and master PHP conditional statement.

Don’t miss out!
Subscribe to My Newsletter
Invalid email address