Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
At this point we've created three new custom post types with their respective custom taxonomies and we populated those custom post types with different types of content. But what's the point of having custom post types with types of content if we don't display them differently depending on what content is there? For example, if we are going to display the content, shouldn't we also be displaying the custom taxonomy information for each of them? This can be done very easily using a feature in WordPress that allows us to create custom templates for each custom post type.
By default, when you run WordPress and you open a post, let's say for instance the Pizza Primavera post, WordPress will use the standard single post template, the same one that's used when you open a regular blog post. But if we create a new file, we can target the recipes post type exclusively, and create a template that's only used for the recipes. To show you how this works I am going to open this Pizza Primavera and I am also going to open one of my photos.
As you see right now the photos and the Pizza Primavera story both look pretty much the same. They have the same elements in them and they have the same layout. What I am going to do now is create a new template that only targets the recipes, so this Pizza Primavera story. I will go into my code editor and under cooking I will create a new file and call it single-recipes.
In this file I need to put in some dummy content, so I will say echo "I am the template file that displays Recipes", end the line, and then end my php delimiter. When I save this and go reload the At the Market photo page, nothing happens. Everything is normal. But when I reload the Pizza Primavera page which lives under the recipes custom post type, we now only see "I am the template file that displays Recipes." That's because we are now using that custom template file and it doesn't have any other content than that line of text.
This is the core way of targeting a specific custom post type for single templates. However, when you are creating a child theme from something like the twentyten theme or the twentyeleven theme you have to do a bit more, and I will show you why. If we go to the twentyeleven theme and we scroll down to the file that calls up single post templates, the one that's called single.php, you will see that inside this file there is a call down here on line 23 that says get_template_part.
This is where the whole loop is going to appear, but instead of having a loop inside the single.php file, it's instead calling a different file within the template that's called content-single and if we scroll up, you will see that here's the file called content-single. There are also a lot of other content files. We have content-aside, content-featured, content-gallery, image, intro, link, page, quote, single, and status as well as one file that's just called content.
This is done to make it easy for people who manage the site to target specific loops rather than the entire page. So the page template here displays the header with the title and everything and displays the footer and then all we are changing is the loop in the middle that actually displays the content for that page. So if you want to make a different loop and you only want it to appear for a custom post type and you are using twentyeleven as your parent theme, rather than creating the single-recipes file you would create the content-recipes file.
But it's not that simple. We still have to make one more change, because as you can see it says get_ template_part content and single. What we needed to do is go test for what custom post type we are using right here. So what I am going to do is grab all the content from the single.php file found under twentyeleven, I'll copy it, then I will go to cooking, create a new file, call it single.php. This file will now override the parent theme, sngle.php, and then I will paste in all the content from the original single.php and go in here where it says get_template_part and take out single and replace it with a function that returns the name of the current custom post type.
That function is get_post_type. If I now save this, delete the single-recipes file, and instead create a new file that I call content-recipes and within it echo a new message, "I am the content template for Recipes," end it, and end that, when I now reload the single page for the Primavera Pizza, which is under the recipes content type, and we will now see the general layout but instead of the loop we just have this text, "I am the content template for Recipes." I will go back to my theme, find the parent theme, in this case twentyeleven, and find the appropriate content template.
In this case it's the one that's called content-single. So I'll open this file, highlight all the content and copy it, go into my content-recipes file, and replace the dummy content with what I just copied. Just to make sure that we are still getting this template and not something else, I will put in a small little div here and just say "I am the content-recipes.php file." I will save this and when we reload this page we should see that new comment I added plus the actual content for the recipe. And we do.
"I am the content-recipes.php file" and here we have the entire recipe. That means we are now at a point where we are working with a template that targets just the recipes content for single pages and we can now make changes and add the stuff we need to add.
Get unlimited access to all courses for just $25/month.Become a member
119 Video lessons · 50728 Viewers
117 Video lessons · 38390 Viewers
113 Video lessons · 81474 Viewers
65 Video lessons · 11347 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.