3.3 Understanding Taxonomy 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.3 Understanding Taxonomy Terms
Hello, and welcome back to this Tuts+ course on custom content types in WordPress. In this part of this course, we'll continue working with taxonomies. And I'll show you how to add taxonomy terms, not only in your admin screens, but also via your plugin. So I've now added some cities. So I've got London, Melbourne, New York, and Sydney. So I'm gonna add a new country for Melbourne and Sydney, because I've got two cities in the same country. Now I've just spotted something here. In my menu, it says country for the taxonomy. I'd rather it say countries. So I'm gonna go back into my plugin for my taxonomy, find that menu name line, and change it to countries. And now it's changed countries when I've refreshed my screen, so that's better. So now let's go into that country screen and we'll add some terms. Now these are taxonomy terms that we're adding here. So I'm gonna add Australia. It'll automatically get the slug of Australia. If I wanted to, I could change that, but if not, I can just leave it. And for the description I'm just gonna write Down Under, not very imaginative, I know, but that's [LAUGH] all I can think of right now. So we now have Australia added, so I can add as many countries as I want. I can also add them while I'm editing my cities. So I could have gone into Melbourne, and then because this is a hierarchical taxonomy I've used, you can see all the countries that are available listed, and I could click on Add New Country to add a new one for this specific post, this specific city. I'm not gonna do that. What I'm gonna do instead, and I'll go back to that country screen so you can see it working, is I'm gonna show you how to add taxonomy terms using code. So if you ever create a plug-in where you want to generate some taxonomy terms for your taxonomy that the user can make use of once they've got the plug-in. So for example, you might be creating a template file for that taxonomy term and you want to ensure that that taxonomy term is there and that it's listed correctly. So it's got the right slug and everything. So you can do this using code. So let's go back to our plug-in. So I'm now adding a function called tutsplus_add_country_terms. And we're gonna copy that. And then again, it's hooked to the init hook. So I'm gonna start off by adding the USA. So the first thing I'm gonna do is check if a term has already been registered for the USA, because if it has, I don't want to create another one. So my term variable is actually just a yes or no variable, true or false. So if the term exists, USA, within my Tuts+ country taxonomy, hang on, I don't put my F in yet. So I'm setting my variable first, and then. So I then want this to be zero and to be null. And now, if that's the case, I'll register the term. And to do this, I use a function called wp_insert_term. So it has a number of parameters, the first one is USA, which is the term itself, the second is the taxonomy. And the third is an array of arguments. The first one is the description. So I'm gonna have that as cities in the USA, cuz that will be at the top of my archive page. And the second one is the slug. So that registers that term for me, and I'm gonna repeat that with the UK. So I've got USA and United Kingdom. I'm gonna save that in my plugin. And then I'll go back to my site, and if I refresh my country screen, you will see that it has added United Kingdom and the USA. So that's another way to add taxonomy terms in WordPress. If your plug-in or your theme needs those taxonomy terms as well as the actual taxonomy, you can add them via the code. So each of your taxonomy terms will have its own archive page. So if I click on View here for Australia, you'll see the archive. Now this isn't working right now, because I haven't updated permalinks. So if I go into Settings and Permalinks and I refresh permalinks, I'm gonna change those to post name and save changes, then I'm gonna go back to Countries. I'm gonna click on View for Australia, and it's not finding anything. Let's have a look. So let's go into our cities and do a quick edit for London, and we'll make that the United Kingdom. Yeah, that's because I haven't added Australia to them here. So let's add Australia. Let's add USA for New York. And Australia again, for Sydney. So if I click on Australia there, I get a list of the cities. So let's go to the country screen. So this is the screen where you add taxonomy terms in the admin, and I can click on View here to see the Australia. So you can see here, we've got country archives, we've got Melbourne and Sydney. So that will be using whatever template file WordPress finds using the template hierarchy, so you could create a template file specifically for that taxonomy term. You could create one for that taxonomy, you could create one for taxonomies in general, or it would simply use your archive.php file, depending on what level of detail you want to go to. Now in this case, it'll be using archive.php or taxonomy.php. In fact, let's take a look at the code, because I'm running 2019 on here and that has an archive.php and it doesn't have a taxonomy.php. So that will be using the same code as any archive file that didn't have a more relevant file in the hierarchy. So it's important to remember that taxonomy terms are similar to categories and tags, a category is a type of taxonomy. A category is a taxonomy, whereas the individual categories you add are taxonomy terms within the category taxonomy. And the same goes for tags. So that's taxonomy terms. In the next part of the course, I'm going to show you how to create a custom template file in your theme for a custom taxonomy. And in this case, we'll change this title up here so that it doesn't say country archives. See you next time and thanks for watching.