Lessons: 7Length: 39 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Creating a Custom Post Type Archive Template

In this lesson, you’ll how to create a template for your theme to display an archive of posts from your custom post type.

Related Links

3.3 Creating a Custom Post Type Archive Template

Hello and welcome back to this Tuts+ course on working with custom post types in WordPress. In this part of the course, I'm gonna show you how to create a custom archive file in your theme to display your custom post type. And I'll also show you how to add your post type to the navigation menu. Now I found my post type by going into Blog, where I added my projects and then finding one here. And then looking at the URL, which has got the domain of my site. You can see that's on a local machine. And then Projects, which is the slug that I registered for my post type and then the individual project. So if I delete that individual project, that will take me to the archive page for my post type. And you need to do it like this, because WordPress doesn't give you this option in the navigation menu screen. So I'm gonna copy that and then go into my menus admin. On my main menu I'm just gonna remove this one, because that's from an old tutorial. And I'm gonna add a custom link over here. So I'll type in Projects, and I've copied in my URL and I'm gonna move that up here. And you can see, if I expand that, there's my link. So I'll save the menu, go back into my site, and there it is. You can see it's grayed out because that's where we already are. So that gives us the navigation menu link for all of our projects. But now let's create that template file to display the projects. Now, as you might have guessed, it's very similar to when we're creating a template file for our single post. So we already used the template hierarchy down here for our custom post to find the singleposttype.php file name. And now we're gonna do the same thing for our custom post type archive. So up here for archives we've got custom post type archive. And that uses archive posttype.php. So it's very similar, but we're replacing single with archive. So I've already created a copy of the archive.php file from my parent theme in my child theme. And I'm just gonna rename that one. So that's called archive tuftsplus_ project.php. So now, let's open that to edit it. Now, I want to make two changes here. The first one is that I want to change my template part that I'm fetching. So I get that loop template file that I created in the previous part of the course. And I also want to change the archive title very slightly. Now, let's get back to the site, have a look at what it is at the moment. So here it says archives projects, I wanna change that so it's a little bit less dry. So at the moment it's using this, the archive title function, now I'm gonna change that, I'm gonna add an echo. And then, instead of this comma here, I'll add my code stops. And then, at the end, I've got the closing H1. But I'm gonna internationalize some content here. So it will echo out H1 plus page title. And then the internationalized form of our project, and then the closing H1 tag. So instead of using the actual archive title, I've got some static text in there, but it's internationalized. But before I save that, I'm just gonna make a little change up here. So that's correct, I'm gonna take out these references to 27team and then as well as changing my heading, I'm also going to change my get_template_part here. So instead of get_post_format here, I use project, just as I did in the single file. Now I don't need to edit that loop file again cuz I've all ready done that. I've already edited with everything it needs. So all I need to do now is change this template file. And by editing that loop file and making it applicable both to single posts and to archives, I've saved myself some work. I've just got one loop and I'm calling it from two different files in my theme. So here we are, I've got Our Projects, and then Project Client 1 Website, Client 2 Branding, and Client 2 Website. So I'm gonna click on those. And that first one and you can see that, that takes me to the single post using that template file I've already created. So that's how you create a custom template file to display all kinds of your custom post type in WordPress. In the next part of the course I'll wrap up and I'll show you what you've learned throughout the course. See you next time and thanks for watching.

Back to the top