For WordPress bloggers, creation of a custom HTML/text widget is not a problem at all. But today I am going to share an interesting trick that will let WordPress users to display multiple widgets(conditional) under the single sidebar widget area. You can use WordPress custom widgets to place Facebook like boxes, Social Subscriptions widgets , AdSense ad widgets etc. Recently one of my client asked me that he want to display 2 different Ads under same sidebar widget. Actually he was using Media.net ads and Google AdSense ads on his website.

His requirement was to show media.net ads on Single Post sidebar widget and AdSense ads (under the same widget) on Homepage and Archives Page. It was done quite easily and I though why not share this trick with my readers. So, let’s come to the implementation.

WordPress Conditional Widgets

WordPress Conditional Widgets

Showing Multiple Ads under Custom WordPress Widget

WordPress allow users to create custom WordPress widgets with text or HTML code. You cannot directly use PHP conditional tags in WordPress HTML/text widget. Here we have to apply conditions like if the user is on Home Page then display Adsense Ads and if he is viewing a Post then display Media.net ads or whatever else. So, in order to include PHP conditions in WordPress HTML widgets, we will install a plugin named PHP text Widget.

So, after installation of this plugin and activation, you will be able to add PHP codes in sidebar widgets. Now you have to use WordPress PHP conditions according to your requirements. Here are some examples.

If you want to display Adsense Ads in WordPress custom Widget when user is on Home Page or Achieves and Media.net ads on Single Post then place the following code in WordPress HTML/text widget.

Example 1

<?php if ( is_single() ) { ?>
    //your Widget code(HTML) for Single Posts

<?php } elseif ( is_home() ) { ?>
    //Your Widget Code for Home page

<?php } else { ?>
    //Your everything else HTML code (for archieves,tags,author page etc) 

<?php }?>

 

You can also use other PHP conditions according to your requirements. Here Is another example that displays Facebook like box under widget when user views a Page and AdSense Ads when user is on Single Post.

Example 2

<?php if ( is_page() ) { ?>
    //Facebook Likebox Code

<?php } elseif ( is_single() ) { ?>
    //AdSense Ad Code

<?php } else { ?>
    //Your Code for other pages
<?php }?>

A number of PHP conditions can be used and here is the list of most commonly used PHP conditions in WordPress.  Click here to see the complete list of WordPress conditional tags.

 

  • is_home()  – For Home Page
  • is_single() – For Single Post (articles)
  • is_page() – For Pages
  • is_tag()  – For tag pages
  • is_category() – For Any Category of Blog
  • is_archive()  – For Archive Pages
  • is_search()  – For Search Results
  • is_404()  – For 404 Error Pages
  • is_author() – For Author Pages

 

Now its your turn to create custom sidebar widgets for different Pages of your WordPress blog and If you face any problem then feel free to comment below.

 

Share with Friends