[How-To] Use WordPress Conditional Tags

Author : | Category : Wordpress | Last Updated :

No one likes to fill their site with lots of advertisements especially in the homepage. So here are few wordpress conditional tags that can be used to display advertisements in your homepage but not in any other pages or vice versa and lot more can be done with conditional tags.

[box type=”info”]You can use these conditional tags for anything like banners, images, videos, text etc not just for advertisements, But I use it to place advertisements in my blog.[/box]

About Conditional tags (few lines from WordPress.org)

The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. For example, you might want to display a snippet of text above the series of posts, but only on the main page of your blog. With the is_home() Conditional Tag, that task is made easy

Not able to understand? You can see a demo in my blog. You can see ads below the menu in posts but not in homepage so the homepage gets a clean look without much ads.

To display ads only in homepage:

All you have to do is, put the following codes in the place where you want to display the ads and replace “Ad Code Here” with your ad code.

<?php if ( is_home() ) { ?>
Ad code here
<?php } ?>

To display ads only in posts:

<?php if(is_single()) { ?>
Ad code here
<?php } ?>

To display ads only in pages (like about, contact us etc):

<!--?php if(is_page()) { ?-->
<?php if(is_page()) { ?>
Ad Code here
<?php } ?>

To display different ads in homepage and different ads in posts and pages:

 <?php if ( is_home() ) { ?>
Ad Code here /* This is will seen in your homepage*/
<?php } else { ?>
Ad Code here /* This is will seen in your posts and pages, except home page*/
<?php } ?>

For more wordpress conditional tags, you can refer Conditional Tags.

I don’t know if there are some WordPress plugins that will do this for you. Its always good to make codes manually instead of using a plugin. Areย you agree with me?

14 thoughts on “[How-To] Use WordPress Conditional Tags

  1. To make the home page much sophisticated, these conditional tags are really very fruitful. Thanks for sharing this informative post.

  2. It seems very simple and I too feel that blog with too much ads looks spammy and I don’t visit them again.

  3. Conditional tags are very important especially when you want to show specific things in certain sections, like advertisements.

    1. Yea, I am using it. It’s really cool ๐Ÿ™‚

  4. Great share Praveen … Conditional tags are really helpful to manage ads !!

    1. I’m glad to know it was helpful ๐Ÿ™‚

  5. Great job Praveen. I think these conditional tags are very important for placing ads inside the post.

    1. Yea! but don’t forget you can use these tags anywhere ๐Ÿ™‚

  6. Conditional tags are very important thing for advertise in application and really this is very good information and thanks for sharing it.

  7. Conditional tags are very useful in development of attractive wordpress site, Thanks Pravin for this useful information and collect as much information about Conditional Tags in one article

  8. Conditional tag helps in maintaining clean advertisement space and attachment of media files in single posts or pages.

  9. Nice Share Isha..these conditional tags can do work of plugins which are normally used for this purpose by many people out their including me also.. it will be good if we use these tags instead of plugins which slows down loading also..

  10. It is nice way to reduce ad plugin from WordPress blog.

  11. It is required to use conditional tags for our site development. All know about plugins and tags but no one know how to use these tags. here you gives very good explaination about how to use these conditionals tags…

Comments are closed.

Digitalocean Banner