Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the great things about working with computers is that you can give them conditional statements or if/then instructions and have them do different things depending on what info they are provided. This is much used in web code because it gives designers and developers the ability to display different content depending on either what the browser is displaying or what the user does with it. In our case we can use conditional statements to display taxonomy links only if they're actually populated so we don't end up with something like the text :Meal type:: with no links next to it.
To do this we're first going to ask the server, if there are any taxonomy terms available before we actually try to display them. If you've been paying really close attention, you may have noticed that there is one taxonomy item missing on this list. We have Meal type, Servings, Difficulty, and Ingredients, but we're missing that item that was called preparation time. So let's add the Preparation time but make it conditional depending on whether or not a preparation time is actually entered. So I'll go back to my file and first I'll just add the standard code. I'll just copy the first list and paste back in, then I'll make two changes.
I'll change the title to Preparation time and then I'll go and ask for the time taxonomy rather than Meal type. If I test this in the browser you should see it now says Meal type and then Preparation time and then Servings. But if we go into Pizza Primavera from behind the scenes and under Edit take out the Preparation time and save it, when we reload the page we'll now get Preparation time with no information.
That just doesn't look right. So we need to add that conditional statement in. So what we're going to do now is use that same function, get_the_term_list, and then tell WordPress to test whether or not get_the_term_list actually returns anything before we create the output. So I'll start right before the div, add a new line, and then I'll start with a PHP delimiter, and I'll make an if statement. So I'll say if (), then I'll say get_the_term_list, and it's still going to be the list from $post->ID and I'm looking for the time term, so it will be 'time'.
And what we're saying is if this does not return, so exclamation point means not and equals means equal, so if this, the term is not equals null, which means nothing, then display what's underneath. So I'll tab that in one so it's easier to read. And then we'll end the if statement with another PHP delimiter and I'll just end the curly bracket. So again what happens here is WordPress is asked if get_the_term_list return something other than nothing then display this.
However, if it does return nothing do nothing. If I save this and reload the page, and you'll remember right now it's empty, we now get nothing. So it says Meal type, Servings. But if we go back and enter a Preparation time, and save it and then reload the page again, Preparation time appears. Now that you see how well it works you're probably thinking the exact same thing I was when I built this.
This conditional statement should really be applied to all my custom taxonomies, just in case I forget to fill some out. So that's what I did in the code-snippets file. So if you go to the code-snippets file you'll find the entire stack of taxonomy displays with conditional statements for every single one. So we can copy this, go back and paste it in, and now after saving it and reloading the page, because all the taxonomies are filled out the old display, but if we go in and take one out, for instance Meal type, and update it you'll see that rather than displaying meal type and the nothing, Meal type just as disappears.
Subtle but important, and it brings in that extra level of professionalism. Using simple conditional statements we can use more consistent and less confusing output on our pages. The seemingly simple addition of a condition that prevents empty taxonomies from being displayed can be powerful because it helps avoid confusion and the perception of an error for the visitor.
Get unlimited access to all courses for just $25/month.Become a member
120 Video lessons · 58330 Viewers
119 Video lessons · 67592 Viewers
84 Video lessons · 16875 Viewers
125 Video lessons · 29769 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.