LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Tips » For Newbies: WordPress Conditional Tags Basic Guide And Tutorial

For Newbies: WordPress Conditional Tags Basic Guide And Tutorial

WordPress-Conditional-Tags-Basic-Guide-And-Tutorial
WordPress comes with a lot of features and its based upon PHP language so its means that we can make him to do more work behind before viewing it to visitor. As you know about conditional tags or if/else statements that are available early in all web languages so same as in PHP that is in WordPress. Official WordPress wrote many conditional tags in the coding so it is now more easy as we just have to use them in our statements.

What Are Conditional Tags?

Conditional tags are conditional statement that originally depend upon `if/else` that you have seen in JavaScript, JQuery, PHP, .Net and many other language. These codes first check the first statement and if that not happening then it doesn’t run the inside codes of this statement and goes to directly second statement. There is no limit of one or two but a Conditional Tags can have unlimited condition and statement so it depends upon you.

Some Default PHP Conditional Tags:

Here as you know that WordPress is depend upon PHP language so first you should know about PHP Conditional Tags then we will proceed to WordPress Conditional Tags. Here below we have to examples of conditional tags that will let you to understand it in more easy way. See that these codes are checking first that is 10=10 then it will run codes inside statement.

<?php
if(10 == 10): 
 echo 'This is true, and will be shown.';
endif;
?>

Now see that these codes are checking first that is 10=15 that is not possible then it will not run codes inside statement and you will get blank space.

<?php
if(10 == 15): 
 echo 'This is false, and will not be shown.';
endif;
?>

Now proceed to if/else both condition that is a real conditional tags. Now check out below codes that is in one piece. First it is checking if statement that 10=11 that is not possible or equal so it will leave to run codes inside it and go to final else statement where it will not check anything and run the codes inside it.

<?php
if(10 == 11): 
 echo 'This is false, and will not be shown.';
else:
 echo 'Since above is not true, this will be shown.';
endif;
?>

Now proceed to if/elseif/else conditions that is an advance in conditional tags but also more valuable. Now check out below codes that is in one piece. First it is checking if statement that 10=11 that is not possible or equal so it will leave to run codes inside it and will go to elseif statement that is also checking that 10=15 that is also not equal so it will also leave to run codes inside it and go to final else statement where it will not check anything and run the codes inside it.

<?php
if(10 == 11): 
 echo 'This is false, and will not be shown.';
elseif(10 == 15): 
 echo 'This is false, and will not be shown.';
else:
 echo 'Since none of the above is true, this will be shown.';
endif;
?>

WordPress Conditional Tags:

Now here we comes to original article about WordPress Conditional Tags. 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.

There are many conditional tags of WordPress that you can view on WordPress Codex Page Of Conditional Tags. Some of most use WordPress Conditional Tags we are hsharing below. Rest you can find on Codex page. From There you can get your desired tags and can use in below statements.  Now There are two way to use WordPress conditional tags. So we will consider both.

  • 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

This one is a full PHP codes means it start with PHP tag and end on PHP tags here you have to use also PHP codes inside your statement whatever you want to run in if/else statement. Means If you want to show some text then you have to use echo’ ‘: tags that can be hard for newbie and also sometime for pro.

<?php 
if( is_home() ):
 echo 'User is on the homepage.';
else:
 echo 'User is not on the homepage';
endif; 
?>

This one is second doing the same work as above but here you can use HTML/JavaScript codes directly in if/else statement without in PHP tags.  Its very easy for newbies when they have to use some HTML/JavaScript codes inside statement so.

<?php if( is_home() ): ?>
 <p>User is on the homepage.</p>
<?php else: ?>
 <p>User is not on the homepage.</p>
<?php endif; ?>

So we recommend to use second type Conditions tags for WordPress. You can get all WordPress conditional tags from there codex page that link we shared early in above paragraph.

Two WordPress Conditional Tags In One Statement:

Now above we shared is one conditional tags. Now if you want to run two conditional tags in one post then how you can do it we are going to share it below.

This is the condition tags where you can add two statement and allow code to check any of one in both and run the codes inside this statement.

<?php if( is_single() OR is_page() ): ?>
 <p>The user is reading a post or a page.</p>
<?php endif; ?>
<?php if( is_single() || is_page() ): ?>
 <p>The user is reading a post or a page.</p>
<?php endif; ?>

This is the condition tags where you can add two statement and allow code to check both condition and run the codes inside this statement.

<?php if( is_home() AND is_page('1') ): ?>
 <p>The user is at the home page and the home page is a page with the ID 1.</p>
<?php endif; ?>
<?php if( is_home() && is_page('1') ): ?>
 <p>The user is at the home page and the home page is a page with the ID 1.</p>
<?php endif; ?>

Inverse Of WordPress Conditional Tags:

Here we want to let you know one more thing that What Is The Inverse Of Conditional Tags? So if you want to show any of your desired result that should be shown when your statement is not true so this can also be done. You just have to follow all upper procedure and just have to add ! before any WordPress Conditional Default Tag that will make it work inverse. Below is the simple example for this inverse conditional tags that we actually shared above in second example.

<?php if( !is_home() ): ?>
 <p>User is not on the homepage.</p>
<?php endif; ?>

And if you want inverse in two WordPress conditional tags then you can do this also as below is the example.

<?php if( !is_single() OR !is_page() ): ?>
 <p>The user is not reading a post or not a page.</p>
<?php endif; ?>

And more if you want inverse one statement in two WordPress conditional tags then you can do this also as below is the example.

<?php if( !is_single() AND is_page() ): ?>
 <p>The user is not reading a post but reading a page.</p>
<?php endif; ?>

Last Words:

This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

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