[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?

Loading comments...

Digitalocean Banner