Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Creating a basic custom taxonomy is not all that different from creating a basic custom post type. All you have to do is create a new function that defines the new custom taxonomy and define a name and type of taxonomy and WordPress will do the rest for you. And of course, all this is done in that post_types.php file. So first let's open it and scroll down to the bottom where we'll start it, and then we're going to add to new custom taxonomy functions. Now like before, I added them into the code snippets file, so it's easier for you to manage, because it's a lot of text and I don't want you to have to write everything.
So if you go to Chapter 4, 02, you'll find In posttypes.php. Here we have the complete function. So I'll copy it out, paste it in, and then we'll take a closer look. As before it starts off with the comment that explains what this is. This is a function that creates custom taxonomies. Unlike the custom post types, this is one function that will create all the taxonomies. So as we add new taxonomies, we're adding them inside the same function, and you'll see how that works in a bit.
As before, it starts within add_action that adds a new action at init, which is one where it starts, and the function that's being called is cooking_create_taxonomies, which is the function directly below. Inside the cooking_create_taxonomies function, we have a new register_taxonomy function that has an array that displays a specific taxonomy. I've commented in what taxonomy it is because, as I said, we'll add multiple taxonomies within the same function. So I've added Meal type because that's the taxonomy, and then I've added the basic array.
So you'll see it says resister_taxonomy. It defines the taxonomy name as meal-type. Now this is the machine name, so this is what WordPress will refer to. And then inside there is an array that points to whether or not it's a hierarchical taxonomy. It is, because it's the Meal type. It gives it a label, so Meal type. It has a singular_name, 'Meal type', show_ ui, which is the UI for the taxonomy itself, true, query_var => true, so you can query it, and then finally rewrite and then I set the slug to meal-type.
So if you remember back to when we created the post types, you'll see that the stuff is pretty much the same. It's just that it's inside a register_ taxonomy function instead of create a new post_type function. If I save this now and go to WordPress > Admin and I reload it, you'll see that nothing has changed. There is simple reason for this. The taxonomy is a sorting system for a post type, but so far if we look at our function, we haven't assigned this taxonomy to a post type yet.
Although the taxonomy exists, it's not hook to anything, so we can't organize anything based on it. That's the next step.
Get unlimited access to all courses for just $25/month.Become a member
119 Video lessons · 50758 Viewers
117 Video lessons · 38457 Viewers
113 Video lessons · 81490 Viewers
65 Video lessons · 11388 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.
Your file was successfully uploaded.