Lessons: 7Length: 44 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.1 Using Conditional Tags to Output Content

Sometimes you want to change the way your template is rendered depending on what type of content is being shown. If the differences are small, conditional tags may be the best choice. In this lesson, you’ll learn how to use conditional tags to check what content type is being displayed and output it accordingly.

Related Links

1.Introduction
1 lesson, 00:37

1.1
Introduction
00:37

2.Making Use of Include Files
2 lessons, 16:58

2.1
Review of Standard Include Files
05:22

2.2
Creating Multiple Loop Files
11:36

3.Creating Custom Template Files
2 lessons, 16:25

3.1
Custom Page Templates
05:23

3.2
Templates for Custom Content
11:02

4.Conditional Tags
1 lesson, 06:37

4.1
Using Conditional Tags to Output Content
06:37

5.Conclusion
1 lesson, 02:58

5.1
Conclusion
02:58


4.1 Using Conditional Tags to Output Content

Hello and welcome back to this Tuts+ course on coding advanced theme templates. In this part of the course I'm gonna show you how to use conditional tags in your theme templates. And that will help you reduce the number of template files and make things a bit more efficient. So let's take a look at the files we've already got. So in the last part we created extra files for our new post type and our new taxonomy. In this part of the course, we're actually going to remove those. And we'll place them with conditional tags in the next file down or up in the hierarchy, depending on which way you look at it, to check what the post type is that's either being output as a single, or the taxonomy that's being archived. So let's start with our single one, we created this file called single-tutsplus_book.php to output a single file of that post type. Now, I'm gonna delete that. And then I'm gonna edit my single.php file. So I'm gonna add a conditional tag to this. So I'm gonna use the is singular conditional tag. So making sure I type it correctly. Now, I have to use a parameter for that, which is my post type. And then I'll open my brackets, and I'm gonna copy this, And edit that single to be book. So if we're on a singular post of the post type tutsplus_genre, It will get the loop file for a book. If we're not, It will get the standard single loop. So I now have a conditional tag that replaces a whole template file. Because all that template file did was use a different get_template part. If you wanted to be really specific, you could actually wrap your conditional tag around this book. Or you could use your conditional tag to define a variable, that's the post type. But I think that's going a little bit too far, I think this is enough. So if I save that and go back to the site, here's my book, and it's not doing it correctly. Let's take a look. That's because I used genre instead of book. Genre is the taxonomy that I'll be working with next. So again I'll save that, refresh the page, and there we are. So you can see what it did after I'd removed the page template for this particular post type. It reverted to using the standard loop for a single page, or for a single blog post. But here I'm using the correct loop. So I've got my genre listed, I've got everything from the loop that I coded in the previous part of the course. Now let's do the same thing for our archive. So we're gonna delete this taxonomy-tutsplus_genre file. And then let me show the effect that has on the site. So you see this doesn't look how it should right now, it's using the archive.php file instead. So let's go back and edit that. Now I'm gonna be cheeky. Copy that so I've got some. My conditional tag, or at least my structure there. I need to change the specific conditional tags. So I need to change this is-singular to is-tax. And then that needs to be tutsplus_genre, which is correct now. I changed that template part to genre. This one to archive. And then I remove that one completely. So if we're in a taxonomy archive for that tutsplus_genre taxonomy, then it'll get the template part, the loop file for genre. If not, it'll get the archive. So let's save that, refresh our archive. And there you can see it's displaying correctly. So that's how you can use conditional tags in WordPress to make your code more efficient. And this page on the codex gives you a full list of all the conditional tags that are available to you. I find them really powerful. You can actually use them in lots of very flexible ways. Theoretically you could just create one index.php file and then output the loop differently with different content types using a conditional tag. I wouldn't recommend that because it does get a little bit unwieldy, that index file. But as you've seen, adding a couple of extra lines in our archive file here has reduced the number of files we've got in our theme and just makes things simpler. And if you wanted to add another taxonomy at a later date, you could add another function here. So let's say I had a taxonomy called foo. So I'd use elseif, and I created a loop for foo, and then so it checked first of it genre, include that. If it's not, it'll check if it's foo and it'll use this loop. And if it's neither of the above, it'll use the standard one. Let me just remove that so it doesn't throw up an error, and that's my new archive.php file. So that's how you use conditional tags within your WordPress themes. In the next part of the course, we'll wrap up and I'll take you through everything we've done during the course. See you next time and thanks for watching.

Back to the top