Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
With the custom taxonomy created, it's time to attach it to some post types. What's cool here is that you can attach the same taxonomy to multiple different post types so that a visitor can get results from several different post types when navigating based on the taxonomy items. Actually hooking a taxonomy to a post type is very easy and I'll show you how. If we look at our current function, the registered_taxonomy function, it says meal-type and then it says array and it has the entire array of all the variables.
If we now simply go in right before the array and add a post type name, so I will call recipes and then add a comma and save this, we can now go to the browser, reload it, and you will see that under Recipes we now have the taxonomy Meal type. You can go in and administer Meal type taxonomy just likely would otherwise, and you can go in and Add new recipe, and you will see Meal type appears on the side where you would normally see a category. But if we go to Photos or Videos, you will see we still don't have that taxonomy here.
So what we need to do now is instead of adding a single post type, we will add an array of post types. An array is simply what you see here. It says array(, and then you have a list of items. So what we can do is replace this recipes with an array, and then inside the array we can put our post types. So I will say, 'recipes', and 'photos', comma again, and finally videos.
Now it's very important here that you retain all of these quotation marks, that you have commas everywhere, and that you put this in the exact order you need it, because if you make any mistakes here, it will take down your entire site. It's simple to fix, but even a simple spelling error here could cause some thing that looks like a catastrophic failure. I will save this, reload the page, and you'll see the Meal type taxonomy now appears under Recipes, Photos, and Videos. We can also attach it to Posts, if we want to.
If you go down here you will see that right now we have Categories and Post Tags. If you go into our array again and simply add post, save it, reload admin, and you will see Meal type now also appears under Posts. Now you can see what happens here. We create a simple taxonomy and then we attach that taxonomy to the post types we want to attach it to and then everything automatically works. You can also take them out again if you don't want. For instance, I don't want the Meal type taxonomy to be attached to post so I will simply take it out from my array, save it, go back to admin and it's gone again.
So the true power here is that you have complete, detailed control of each item and where it appears. With a taxonomy hooked to several post types, we can now fully test and populate them with content. More importantly, we can also attach them to new posts for easier sorting. But this is a hierarchical taxonomy. What happens if we want to introduce a nonhierarchical taxonomy? That's the next step.
Get unlimited access to all courses for just $25/month.Become a member
120 Video lessons · 58611 Viewers
119 Video lessons · 67738 Viewers
84 Video lessons · 16995 Viewers
125 Video lessons · 29887 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.