Check Your Internet Speed

Conditional Tags WordPress

One of the best features of the WordPress could be the conditional tags. It allows you to tell the code to act differently in specific situations. For example, you can check if the user is using Windows or Mac, and display different content based on the systems. You can also redirect to post if search query only returns single result. You name the situations, the conditional tags can recognize them all!
wordpress conditional tags
Despite its flexibility on determining actions based on different situations, it’s also extremely easy to learn, and there are even tutorials and resources spread over the web for you to actually master it. That said, in this article we’ll go through a detailed introduction about the conditional tags, how they work and when to actually use them.
In the last section of the article we’ll also show 10 useful snippets for you to achieve the most with conditional tags, so get them all to make your WordPress site acts more intelligently to unique situations!

If (Statements)

With PHP if statements you can ask if something is true or false, 1 or 0. If your statement is true, your code will be executed, and if it’s false nothing will happen, depending on how you decide the actions in the conditional tags. Check out the example, and I’m sure you’ll understand what I’m talking about.
  1. <?php  
  2. if(10 == 10):    
  3.     echo 'This is true, and will be shown.';  
  4. endif;  
  5.   
  6. if(10 == 15):    
  7.     echo 'This is false, and will not be shown.';  
  8. endif;  
  9. ?>  
You can also use elseif which lets you add another statement, and else that will be executed if your first statement is false.
  1. <?php  
  2. if(10 == 11):    
  3.     echo 'This is false, and will not be shown.';  
  4. elseif(10 == 15):    
  5.     echo 'This is false, and will not be shown.';  
  6. else:  
  7.     echo 'Since none of the above is true, this will be shown.';  
  8. endif;  
  9. ?>  
That’s all you need to know about if statements for now, let’s get into WordPress conditional tags! However, if you want to dig deeper into PHP if statements, head over to php.net for reference.

How Conditional Tags Work?

When using the native WordPress function like is_home(), you simply ask WordPress if the user is currently on the home page or not. WordPress will then answer with 0 for no, and 1 for yes.
  1. <?php   
  2. if( is_home() ):  
  3.     echo 'User is on the homepage.';  
  4. else:  
  5.     echo 'User is not on the homepage';  
  6. endif;     
  7. ?>  
For a complete list over WordPress conditional tags you can visit their codex.

Combining statements

There are cases when you might want to check more than one statement. This is easily done by using AND and OR.
  1. <?php   
  2. if( is_home() AND is_page( 5 ) ):  
  3.     echo 'User is on the homepage, and the home pages ID is 5';  
  4. endif;     
  5.   
  6. if( is_home() OR is_page( 5 )):  
  7.     echo 'User is on the homepage or the page where the ID is 5';  
  8. endif;  
  9. ?>  

When To Use Conditional Tags?

Conditional tags are great when you want to change your content depending on the answers of question relevant to your site. Is the user logged in? Is she using Internet Explorer? Is there any post to be shown?
To get an example of conditional tags in use, we can look into Twenty Eleven’s (the standard theme in WP 3.2) index.php, line 20.
  1. <?php if ( have_posts() ) : ?>  
  2. ... posts ...  
  3. <?php else : ?>  
  4. ... search field ...  
  5. <?php endif; ?>  
This checks if there is any post to show, and if the answer is no, the search field is displayed.
Below is another example of WordPress conditional tags:
  1. if( is_admin() ):  
  2.     # User is administator  
  3. endif;  
  1. if( is_home() AND is_page('1') ):  
  2.     # The user is at the home page and the home page is a page with the ID 1  
  3. endif;  
  1. if( is_single() OR is_page() ):  
  2.     # The user is reading a post or a page  
  3. endif;  
  1. if( !is_home() AND is_page() ):  
  2.     # The user is on a page, but not the homepage  
  3. endif;  

10 Useful Conditional Tags

The conditional tags available in the WordPress codex page are pretty limited to the big parts of WordPress, like posts, pages and such. There are, however, a lot of small and useful statements available if you look around the web.

Check if user is logged in

This will be a handy snippet if you have a blog with users registered, as it checks whether your user is logged in or not.
  1. if ( is_user_logged_in() ):  
  2.     echo 'Welcome, registered user!';  
  3. else:  
  4.     echo 'Welcome, visitor!';  
  5. endif;  
[Source]

Show content if registration are opened / closed

A good snippet if you have user registration feature in your site, and you want to let visitors to know about whether the registrations are opened or closed.
  1. <?php if ( get_option('users_can_register'):  
  2.      echo 'Registrations are opened.';   
  3. else:  
  4.     echo 'Registrations are closed.';   
  5. endif;  
[Source]

Check if user is on a Mac or a PC

Want to provide specific content based on the Operating System that users are using? Here’s the snippet for you.
  1. ifstristr($_SERVER['HTTP_USER_AGENT'],"mac") ):  
  2.   echo 'Hello, I'm a Mac.'; 
  3. else: 
  4.   echo 'And I'm a PC.';  
  5. endif;  
[Source]

Disable Google Analytics for logged in users

If you are using Google Analytics and you only want to track the visitors other than your authors and writers, you can probably use this snippet to achieve the purpose. Be sure to change UA-XXXXXXX-X to your Google Analytics ID.
  1. <?php  
  2. // function for inserting Google Analytics into the wp_head  
  3. add_action('wp_footer''ga');  
  4. function ga() {  
  5.    if ( !is_user_logged_in() ): // íf user is not logged in  
  6. ?>  
  7.     <script type="text/javascript">  
  8.       var _gaq = _gaq || [];  
  9.       _gaq.push(['_setAccount''UA-XXXXXXX-X']); // insert your Google Analytics id here  
  10.       _gaq.push(['_trackPageview']);  
  11.       _gaq.push(['_trackPageLoadTime']);  
  12.       (function() {  
  13.         var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;  
  14.         ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';  
  15.         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  
  16.       })();  
  17.     </script>  
  18. <?php  
  19.    endif;  
  20. }  
  21. ?>  
[Source]
Source:hongkiat.com

0 comments:

Post a Comment

Websites Resources | Blogging | Technology News | Softwares - i Developments