LATEST >>

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

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Tips » 10 Useful WordPress Conditional Tags For Template Designers

10 Useful WordPress Conditional Tags For Template Designers

10-Useful-WordPress-Conditional-Tags-For-Template-Designers
As you know that WordPress have its own Conditional tags that are predefined in WordPress and you just have to use it. If you don’t know about WordPress Condition Tags then you must have to real our previous article at For Newbies: WordPress Conditional Tags Basic Guide And Tutorial where you can know the basic of WordPress Conditional Tags and also can generate your own desires in any way.

So now here we are sharing some ready made WordPress Conditional Tags that will clear your mind out about tags and also help you a lot as they are ready to use in your templates. Checkout our this list of 10 useful WordPress Conditional Tags and use it.

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.

<?php if ( is_user_logged_in() ): ?>
 <p>Welcome, registered user!</p>
<?php else: ?>
 <p>Welcome, visitor!</p>
<?php endif; ?>

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.

<?php if ( get_option('users_can_register'): ?>
 <p>Registrations are opened.</p>
<?php else: ?>
 <p>Registrations are closed.</p>
<?php endif; ?>

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.

<?php if( stristr($_SERVER['HTTP_USER_AGENT'],"mac") ): ?>
<p>Hello, I'm a Mac.</p>
<?php else: ?>
<p>And I'm a PC.</p>
<?php endif; ?>

Show Different  Data On First Page And All Rest Page:

If you want to displat different data/codes on your blog first page only and other different data on all rest of the pages except first page then here is the snippet for you.

<?php if(is_home() && !is_paged()): ?>
<p>Add you codes here to show it only on first page of blog.</p>
<?php else: ?>
<p>Add you codes here to show it on everypage except first page of blog.</p>
<?php endif; ?>

Check If Post Is Custom Post Type:

Using the conditional tag below you can check if the current post is in a specific custom post type, for example, books.

<?php if ( is_singular( 'books' ) ): ?>
<p>This post is of the custom post type books.</p>
<?php endif; ?>

Check If Last Post:

The chances are if you’re using separators between your posts you probably don’t want to include it into the last post of the page. Include the conditional tag below into your loop where you want to display something only if it’s on the last post.

<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) { ?>
 <div class="separator"></div>
<?php } ?>

Check If Current User Can:

There are times when you want to know the role of the users, for example you only want certain links (edit, etc) to be displayed for authors and here is the code:

<?php if( current_user_can('editor') ): ?>
<p>true if user is an editor</p>
<?php endif; if( !current_user_can('administrator') ): ?>
<p>true if user is not admin</p>
<?php endif; if( current_user_can('edit_posts') ): ?>
<p>true if user can edit posts</p>
<?php endif; ?>

Check If User Is Coming From StumbleUpon:

StumbleUpon is a great social media which is able to attract traffic to your site. Here’s a trick to attract the Stumblers: display a special message to welcome them by using the conditional tag below which checks if the user comes from StumbleUpon.

<?php if( strpos($_SERVER[HTTP_REFERER], "stumbleupon.com" ) == true ): ?>
<p>Hello StumbleUpon user!</p>
<?php endif; ?>

Show Data On First Page Only:

StumbleUpon is a great social media which is able to attract traffic to your site. Here’s a trick to attract the Stumblers: display a special message to welcome them by using the conditional tag below which checks if the user comes from StumbleUpon.

<?php if(is_home() && !is_paged()):?>
<p>Add you codes here to show it only on first page of blog.</p>
<?php endif; ?>

Show Default Excerpt If Not Available:

If your post only contain an image and no text so your blog main page will be messed up as it will hide space of text and rest will be messed up so in this case you can write some your default text fro every without text post.

<?php if(! has_excerpt()): ?>
<p>Add you desired text here to show it on without text post excerpt.</p>
<?php else: ?>
<?php the_excerpt(); ?>
<?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.

Recommended For You:
How To Delete All Spam Comments In WordPress By One Click?

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 *