7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 10Length: 1.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Post Type Template Files

In this lesson, I'll show you how to use template files in your theme to display single posts and archives for a post type.

Related Links

2.3 Post Type Template Files

Hello, and welcome back to this Tuts+ course on custom content types in WordPress. In this part of the course, we're going to continue working on custom post types. And I'll show you how to create a template file for displaying your custom post types. So what we'll need to do is create a child theme, in this case. Because I'm using the 2019 theme, I'll need to create a child theme for that theme because I want to edit it and add a new file. And you should never add a new file to a third party theme, because when you update the theme, you'll lose that. You also shouldn't edit the files in the theme. So if you're working with your own theme and you can edit that, that's absolutely fine, not a problem, just work with that theme now. But if you're using a third party theme, you'll need to create a child theme. So here's my theme, so let me just quickly show you the code. There's a style sheet that tells WordPress that this is a child theme because of that template 2019 line. And there's also a functions file that enqueues the style sheet from the parent theme. Now, it's important to do that using the functions file. You shouldn't do it in the style sheet, which is how it used to be done a while ago, but this is the correct way to do it. So, my post type is registered here, and I've just spotted a mistake I made when registering the post type. When I've gone to this post, you can see here, it says tutsplus_city/london. And I used rewrite, so it should say city, so I need to correct that, and that's because the rewrite needs to actually be an array. So it's an array with the slug being city. I'll save that, and then refresh, and it's actually gone because that tutsplus_city/london doesn't exist anymore. So let me just go back into my editing screen, visit the city. And again, it's not showing up and that's because I need to refresh my permalinks. And if you ever have problems in WordPress displaying a screen. Or you find that a post or an archive file that should exist just isn't showing, you're getting a 404 error. You probably need to refresh your permalinks. Because whenever you set up something new to do with slugs or post types or permalinks. You always need to tell WordPress that those permalinks exist by doing this, so let's go back to our Cities. We'll go to London, and we'll View it, and there we are, so that is using a file from the 2019 theme, now let's take a look at that file. So we go back into themes, and 2019, and we're going to be looking at the single.php, now, let me just check the Template Hierarchy. So, a custom post type, if there's a selected file, it uses that template file, a bit like page templates, which we're not using. If it's a single with a particular slug, it will use that, which is not what we want. A single post type is what we're going to be creating, but in the absence of that right now, it'll use single.php. So that's the file from 2019 that we'll be using, so, I'm going to copy the contents of that, and then close it. And then I'm going to go back to my files and my themes, now I created a single Tuts+ city.php file. But I just put the number 1 in front of it, so that I could demonstrate to you then that it wasn't in use. Having done that, if I go back into London, it'll be a blank screen because that file is empty. So what I'll do now is copy in the content of that single post, I'm going to edit this commented out text here. And save it, and then I go back to my city and here we are, so I now want to add something new to it. So you could add whatever you wanted, so you could, but first, I'm going to make a few tweaks. So you can see here, there's a whole section here, if is singular attachment, or else if is singular post. Now, obviously that's not going to be the case, so let's find that code and remove it, and all of that as well. There we go, so we're just getting the template part for the loop, what we'll need to do is create a new loop file as well. So I've already created a new single Tuts+ city file. And then I need to amend this get template part, and I'll change this single parameter here to city. And then what I'll need to do, we've got template parts, content, and then we'll have to have content city, so I'll create a folder. Content city-, sorry, content-city.php. To populate that, we'll need to copy the file from, or the contents of the file, from the 2019 theme. So let's go into template-parts, content, content-single, and again, we'll copy all of that. And we'll paste it there. We now have an article in there, we've got another get template part for the header, which we don't need to worry about. We've got the entry content, the title, we've got pages before and after, and we've got the content here. So let's take a look at our page again, so let's say, for example, we want to add a list of the countries. Or it wouldn't be a list because obviously there's only one, underneath there. And that would use a taxonomy called country that I'll be adding in the next part of this course. Now, for the purposes of this tutorial, I've already added that, and I'll show you how to do it in the next part of the course. But here, I need that data so that I can display that in this template file. So I want to add, the country that the city is in under here in my header. So I'm going to place it in this header element underneath this template part call for the header. And that template part is fetching this information here. Now I could go down another level with this code, and I could put it in this template part here, or I could create a duplicate of that. But for my money, that means creating three files, and I really don't want to do that, so I'm going to add it here. And I'm going to use this function here called get the term list. So I'm going to echo that out, I'm going to close my PHP, and get the term list has a number of parameters. The first one is the ID of the relevant post, so it's this post. The second is the taxonomy. Which is Tuts+ country, as you'll see in the next part of the course, the third is the text that precedes it. And then the fourth is a separator. Now the separator is unlikely to be used here because obviously a city is in only one country. But you never know, so it doesn't do any harm to put it in, so I'll save that. Now, I've already added my countries to my site, which again you'll see shortly. And here you can see it's listed the Country, United Kingdom, now I want to add a space in there, so let's just add that in there and save. You can see that it's United Kingdom, if I click on that, it takes me to the archive. So that's how you edit the single template file for a post type. In the next part of the course, we'll move on to looking at taxonomies. And I'll show you how taxonomies work before we start to register one. See you next time, and thanks for watching.

Back to the top