WordPress allows you to create individual custom post type templates for each of your custom post types. This means you can fully customize the way each of them is displayed. However, it also means if several of your custom post types are supposed to look the same, you have to create several identical files. Though this is easy if you have just one or two, it can become hard to manage if you have several different post types to deal with. For example, if you make a change in one file, you then have to make a change in all the other ones so they all stay the same. There are many ways to deal with this.
And in this course, I'll show you three different methods. The first one is to deal with each individual post type as its own template. If we look at the content we've created so far, we have recipes, photos, and videos, and then on the side we have the regular posts. When we look at the recipes, we've already created a custom post type template that displays our custom taxonomies. But inside photos and videos, we still don't see those custom taxonomies.
And more importantly, the photos and the videos have different taxonomies from the recipes. So we're going to make new custom post type templates for these two other custom post types. We'll start off with photos. I want to add the custom taxonomies to the template, but I also want to display it at the bottom instead of the top, because I want to keep the photos in focus. So let's start off by opening the content-recipes file that we already created and simply re-saving it under a different name. So I'll save it as content-photos.
Now this template file should apply to the photos post type. So I'll simply go back to my browser and reload the photos page and you see we've added the custom taxonomy information at the top. And of course, now that our custom taxonomy information is conditional, it only displays what's available. Because you remember, the photos custom post type only has three custom taxonomies attached: Meal type, Ingredients, and Techniques. So what we need to do now is display the techniques and then move all the taxonomy information to the bottom.
So I'll start off by going in and taking out what I don't need, so I'll delete Preparation time, Servings, and Difficulty. Then I'll copy one of the existing taxonomies because I'm lazy, so I don't want to type all of it again. I'll copy the first one and paste it in directly underneath and then I'll make a change to the top one. So instead of testing for meal type, I'll test for techniques. I'll change the title to Technique and I'll display techniques taxonomy terms.
If I save this, reload my browser, Technique appears at the top here. The next step is to move this down to the bottom underneath the contents. To do that, I'll go ahead and look at my code and find out where the content displays. Fortunately, WordPress has a fairly semantic setup when it comes to functions, so it's easy to find things. For example, when I'm looking for the content, I can find this function called the content, which is what inserts the content.
That means I need to take all this information up here and move it down underneath the content somewhere. So I'll simply cut it out and I'll delete the call to this div, because I don't need it anymore. And then I'll scroll down and see where I can put my custom taxonomy information in a logical place. The logical place is right here, footer class entry-meta, because it's the same thing as before. So I'll simply add a new line under there and paste my information in. Tab it in just to make it look nice and save it.
When I reload my page, the taxonomy information disappeared from the top and instead appears on the bottom here. All that's left for me to do now is to add a small space between Ingredients and the information underneath, so that it looks better. So I'll go back into my code editor and simply add a break. When I reload the page, we now have the custom taxonomy information, a break, and then the rest of the information about the post.
Exactly what I wanted. Just to make sure I didn't mess anything else up, I'll now go back my recipes and just reload the page. And as we can see, everything stays the same, which means we're not working with two different templates. So now we have the recipes templates and the photos template and we finally have to add the videos template. So I'll go back into my code editor, re-save this file under a different name, content-videos, and then all I have to do is change the Technique call to video-types.
So I'll go in and test for video-types first. Change the title to video-types. I will display the terms from the video-types taxonomy. Save it, go to my video post, reload it, and now you see underneath the video, we have the Video type, Meal type, and Ingredients displayed. And just to make sure I didn't mess anything up, I'll go check my photos again.
It still displays Technique, Meal type, and Ingredients. And my recipes still display Meal type, Preparation time, Servings, Difficulty, and Ingredients. We've now created three separate custom post type templates that adhere to each individual custom post type. This is the simplest way of doing it and if you have different custom post types that have very different content, this is the right way to do it because then you can create custom templates that adhere to that type of content. However, in many cases like ours, the differences are very subtle and it would be easier if we just had one file that would then handle everything and then we could just point to that file.
That's the second method we're going to look at. And just a note, in the rest of this course, after we've gone through these three methods, I'm going to be using the third method. So you should go look at that one as well.
Get unlimited access to all courses for just $25/month.Become a member
117 Video lessons · 43219 Viewers
119 Video lessons · 54459 Viewers
65 Video lessons · 14665 Viewers
113 Video lessons · 82959 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.
Your file was successfully uploaded.