Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:11Length:1.2 hours
  • Overview
  • Transcript

4.1 Creating Custom Loops in Template Files

Hello, and welcome back to this Tutsplus course on working with the WordPress Loop. In this part of the course, I'm gonna show you how to create a custom template in your frame to create a custom loop. And I'm gonna do that for custom post type that I've already activated on my site. So I have a plugin here, called Tutsplus plugin to register post type. Now what that does it registers the project post type. So you could see I've got projects here, as a post type over in my Admin menu. And I've gone ahead and created a couple of, well, three dummy projects that will display on my site. So if we take a quick look at the code in my plugins, I've got this Tutsplus plugin to register post type plugin, and here's the code. It's a fairly standard plugin to register post types. But if you're not sure how to do that you can see our video on how to register custom post types in WordPress. So going back to our site, in the front end of the site, I've added a link to my project archive in the main menu. And that will take me to each of the projects. So if I click on that, you can see all of the content. It's a bit dull at the moment, because it's just got Latin placeholder text, but it gives you the idea. Now let's go back to that archive page. I want to customize this archive page. Now I've got two ways I could do that. I could either edit the loop for archives or I could create a custom template file. Now the first thing I'm gonna do is for this one, I'm gonna edit the loop, and then for the individual single project, I'm gonna create a custom template file. So let's go into the code for the loop. And here I've got the loop.php file that we were editing earlier on in the course. So what I want to do is instead of just having PHP the title, which is what's here, I want to change this to our latest projects. So again, I'm gonna use some conditional text. But this time it's not actually in the loop. So the editing I want to do, is in the main archive.php file. So although we're not working in the loop here, it helps to know when you're in the loop and when you're not. And you can see here it's got php the_archive_title. So let's go back to the codex reference on conditional text to work out which one we need to use. So what we're looking for is an archive for a custom post type. So here we have a post type archive. So I've put is _post_type_archive and then my post type. So I'm gonna copy that. I'm gonna copy that text. So if it's a custom post type for project, now let me just double check that that is the ID of that particular post type. So if I go into my plugins and I go into register projects, we'll see, it's actually called Tutsplus project. So I'll copy that and edit it accordingly. So if it's a post type archive for my Tutsplus project post type, I'm gonna echo, and I'm gonna internationalize it. That's better. Our latest projects. And I will put that in title case. And if not, I'm gonna echo out the archive title. And we're just gonna display the archive title using that. And then I finish that up with my closing h2 tag. So let me just get rid of some of the spacings cuz I don't think we need it all. No, actually let's keep it. I like having lots of spacing in my files, so I can see what's going on. So if we're in a post type archive tutsplus project, it'll say our latest projects and that will be internationalized. If not, it'll echo out the archive title, so I'll save that and go back to my site and take a look. And right now, it's not outputting anything there. Now, I've just checked the internationalization tips on the codex and it's because I should be using this function instead. Because it's got to echo a string to the browser. I'm not an expert in internationalization, so if you get stuck on this, I suggest you check that same page. So I'll save that, and then go back to my projects archive. And refresh and there we are. So instead of the standard archive heading we've got our latest projects there. And I did that by editing my archive.php file. So now I'm gonna edit the loop by creating a new file for my single project. So I'll close those and I will go back into my theme folder. And because I'm creating a single one, I'm gonna duplicate an existing one. To save myself, the work of doing everything all over again. So I'm gonna call that, single-project.php. Now let's start out by editing the comment out here. So it's' single projects and you can see there's a call to the single loop here. Now, I'm gonna create a new loop file and I'm gonna call it project. So I'll change that get_template_part function and save it. Now at the moment that won't call anything. So I need to make a duplicate of my loop single file. So I'll duplicate that and call it loop-project.php. I'll edit my commented out text here, the loop for single projects. But before I start editing this file, I'm just gonna fix something I've spotted that I've done wrong so far. This template file that I created, I called single-project. But, when I registered the post type that is tutsplus_project, so I need to rename my single project file otherwise it won't work, WordPress won't pick it up. So let's go back into themes and into my theme, and I'll simply rename that, Simple-tutsplus_project, so that will now work with my tutsplus_project post type. And I'll open that, I don't actually need to make any changes to that, so I can close it down again. So let's go back to our loop project file. Now, what I'd like to do is display, along with the date here underneath, a list of the taxonomies. The terms within a given taxonomy that apply to this post, this project. And I've registered a taxonomy for my tutsplus_project post type. So here in my plugin where I registered the post type, I've added a post type called project types. And I've registered it there for the tutsplus_project post type. So let's add the code that will display that list. So let's take a look at that code. So I'm using the function get_the_term_list, which will display a list of terms applied to this post, this project, with a link to the archives for each of those terms. So it has a number of parameters. The first one is the post Id, which is the current post. The second is the taxonomy tutsplus_project_type. The third is the text that displays before that list, which is an opening paragraph and the word categories. The next one, the fourth, this gets a bit confusing here, cuz you can see all these commas. But you can see how this comma here is in red because it's inside my quotation marks here, and that is the separator. So that's what separates every item on the list. And then the final parameter is what appears at the end, which is my closing pay tag. So let's save that. And there you are. You can see a link to all the terms that apply to this project, which is branding. Now the archive page isn't working yet for the simple reason that I haven't updated by permalinks since registering that. So I need to just refresh my permalinks by doing that. And then I'll go back a few pages, refresh that screen, and there you can see it works. So I've got Client 2 Branding listed under branding. And if I go there, you can see branding is listed under that particular project. So that's how you edit a template file. Or you create and edit a template file in order to create some custom content on a specific post type in your site. You could also do that for any kind of template file. So I've customized the loop here to add a list of taxonomy terms. You could alternatively add other metadata such as the author name or the categories or tags, or you could add additional content. In the next part of the course, I'll show you how you can modify the loop using the pre get post action hook. See you next time, and thanks for watching.

Back to the top