7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Creative Coding

Using Conditional Tags to Supercharge Your Blog

Read Time: 6 mins

Conditional Tags are one of the many great structures that WordPress has to help us develop for WordPress easier. In this article, we're going to get to know some of them and use them in example functions like removing things from the error pages or changing the favicon of admin pages.

What Are "Conditional Tags"?

They are basically "yes-no questions": They return only TRUE or FALSE when you use them. We use them in if statements – if the statement is TRUE or FALSE, we can process our code according to the answer.

You can see all the Conditional Tags in the WordPress Codex.

Now, let's get to the fun part! There are ten great functions using Conditional Tags in this article.

Function 1. Display a Popup Message in the Front Page With is_front_page()

Greeting the visitor from the home page could be pleasant for the visitor, or you can place a warning for scheduled maintenance, or you can show a horrendous popup ad. Whichever you need to do, here's how you do it:

First, you need to get the Colorbox jQuery plugin here. Get colorbox.min.js from the "colorbox/colorbox" folder and the colorbox.css (and the corresponding "images" folder) to a "colorbox" folder inside your theme folder.

Then, you need to create a colorbox.load.js file in order to load the popup. Place this file into the "colorbox" folder, too:

After that, place your popup HTML code (with the "mypopup" ID for CSS) inside your theme's index.php file and hide it in your style.css file (with "#mypopup {display:none;}").

Paste this into your functions.php file and you're good to go!

Note: In order to make your popup go away, you need to add a link inside your popup. This will do just fine:

Function 2. Include Extra CSS and JS Code Inside a Specific Page With is_page()

You may need to load some external JavaScript or CSS files for a specific page – like your "About" page or a download page for your product. Yes, you can also include them in your content but it's not good practice. Here's the good practice:

Like the first example, adding this into your functions.php file is enough. (Don't forget to change the "123" number with your page's ID!)

Function 3. A "More From This Category" Section for Posts in a Special Category With in_category()

It's not always necessary, but you may need a "More From This Category" section for a category (but not the other ones). Say, you have a "News" category and the other categories are not suitable for the section we're going to create. The Conditional Tag in_category() will help us with that:

Build this function as you like and add it into your functions.php file. Then, go to the single.php and place the code (<?php more_from_category(123); ?>) where you want the section to appear. All you need to consider is to place the code inside The Loop. That's all!

Function 4. Remind Yourself (Or Your Authors) That You're Still on the Preview Page With is_preview()

This is not a must (after all, we're just learning examples for these Conditional Tags) but it might be a good idea to remind yourself (or your authors) that the page displayed is the "preview" page. Add this into your theme's functions.php file:

Of course, this is not enough – you need to edit the style.css to give a shape to the warning text. Something like this:

There you go!

Function 5. Remove Certain Elements From Your 404 Pages With is_404()

This one is the easiest tip of all. I don't think it even needs an explanation – just wrap those "certain elements" (things that you don't want to show on your error pages, like ads) with the code below and you're good to go!

Function 6. Never Show Auto-Generated Excerpts Again With has_excerpt()

I just hate the auto-generated excerpts. So I remove them – with the code actually provided from the Codex:

Add this into the functions.php file and then all you have to do is change the instances of the_excerpt() with full_excerpt().

Function 7. List Only the Post Titles (Instead of Full Posts) on Date-Based Archives With is_date()

Sometimes, listing just the titles is more than enough on certain archive pages – like the date-based archives. So, for example the Conditional Tag is_date(), we will get rid of the stuff in The Loop except the title.

This is kind of tricky since the archive.php files are different in each theme. (And if there's a date.php file in your theme, you should edit that one.) Look for The Loop in the code and change the code inside The Loop with this:

Function 8. A Separate Favicon for Your Admin Panel With is_admin()

This tip could be quite handy if you like to work with 20 open tabs, all for your blog. Just edit your favicon a little bit and save it as adminfav.ico – for example, my admin panel favicon is just the red version of my original favicon.

Anyways, here's how you do it:

Function 9. Show a Default Thumbnail if the Post Doesn't Have One With has_post_thumbnail()

This is kind of a must for a good theme. If you have any part in your theme where the featured images' thumbnails are shown, you should replace the the_post_thumbnail() functions with the code below:

This way, you can keep the consistency of your theme's looks.

Function 10. Show a Special Menu for Your Logged in Members With is_user_logged_in()

If you use the membership system in WordPress and have members, you may want to create a special menu just for your logged in members. Here's how:

This is a standard "title & list" code, you should play with the code to make it like your sidebar divs and then place the code <?php member_menu(); ?> in your theme's sidebar.php file.

Also, this is just an example, but ideally you would use WordPress custom menus with wp_nav_menu() here. One standard and one for members, then you can continue to manage them from your WordPress admin dashboard. You can read more about the wp_nav_menu() function here.

Any other ideas?

These were my favourite 10 ideas to use Conditional Tags. What about yours? If you have anything to share, please comment below so we can extend this post with more ideas!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Scroll to top
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.