3.4 Taxonomy Template Files
In this lesson, I'll show you how to create template files in your theme to display archives for built-in and custom taxonomies and terms.
1.Introduction1 lesson, 01:00
2.WordPress Post Types3 lessons, 25:02
3.WordPress Taxonomies4 lessons, 29:39
4.Custom Fields and Post Metadata1 lesson, 05:33
5.Conclusion1 lesson, 02:48
3.4 Taxonomy Template Files
Hello, and welcome back to this Tuts+ course on Custom Content Types in WordPress. In this part of the course, we'll continue working with custom taxonomies. And I'm gonna show you how to create an archive page for your custom taxonomy. So the first thing I'm gonna do is create a child theme for the Twenty Nineteen theme, because I don't want to directly edit the Twenty Nineteen theme itself. So unless you're using your own theme that you can edit, you should also create a child theme of the third-party theme that you're using. And you can find out more about child themes here in the Theme Handbook. So I've made sure that my theme called Tutsplus Custom Content Types is activated. And if we have a look at the code, that's got a style sheet that tells WordPress that this is a theme and what the parent theme is. It's also got a functions file, which enqueues the style sheet from the parent theme. So let's take a quick look at the files in that child theme. Firstly, we have our style sheet with version number 3.4 and template twentynineteen. And then we have our functions file where we're enqueuing the parent style sheet. And that's where you should enqueue your parent theme style sheet, not in the style sheet of your child theme. I've also created a new file which at the moment is blank. And that's called taxonomy-tutsplus_country.php, and the title for that I get from the WordPress template hierarchy. So here for a taxonomy, so I have a custom taxonomy archive. First I can create one for an individual term, which I don't want to do. And then for an individual taxonomy, and I call it taxonomy-, and then the name of the taxonomy, .php. So that's what I've done. Now, at the moment, that's empty. So what I'm gonna do is take the contents of the archive.php file from my parent theme, and edit those. Now, you would do this differently depending on your parent theme, or depending on the theme that you're using if you're editing your own theme. So if you're editing your own theme, you'd make a duplicate of the archive.php file or of the taxonomy.php file if you've got one, and then you'd edit that. But if like me you're using a parent theme, you copy it from the parent theme. So I'm gonna go into my parent theme, twentynineteen. I'm gonna open archive.php, copy it all, and then paste it into here. So I'm gonna make a few edits. So at the moment, the code generating any output is exactly the same as if I was just using that archive.php file. So I want to change that. So let's take a look at the archive in the site, so I'm gonna view Australia. At the moment, it says Country Archives, and it doesn't give us the name of the country, which I have to say is a little bit odd. So I'm going to add that in. So instead of having the archive title here, I'm gonna use something called get_queried_object. So first I'm gonna define a variable. Making sure I type it correctly. So I've got a variable called term, which is the queried object. Now, if I was to try and echo that out, I would get a huge dump of loads of information about the queried object. So what I need to do is echo out specifically the name of the current queried object. So I'm gonna echo out h1 class = page-title. And then my closing h1 tag. And I'm gonna get rid of that line there for the archive title. And I'll save my file. And if I refresh my archive, it now says the name of the country at the top. So I could change anything I wanted to in that archive file. I could change the layout, I could add some extra CSS classes, and then target those in my style sheet if I wanted to. I could change colors. I could change the specific way that the loop is run, although I would need to create a new template part if I wanted to do that. Or I could add more in here in my header. So that's how you create a template file for a taxonomy. And you do it by copying an existing file that's higher up in the hierarchy and editing it so that it works the way you want it to. In the next part of the course, we're going to be moving on to look at custom fields for posts, also known as post metadata. And I'll show you how to add those in the admin screens and also how to display them in your template files. See you next time, and thanks for watching.