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

3.2 Coding the Loop in Single Pages and Posts

Hello, and welcome back to this test plus course on working with the WordPress Loop. In this part of the course, I'm gonna show you how to create a custom loop for a static page or for a single post. So for any kind of single content instead of an archive, and I'm gonna do this slightly differently. You might remember that in the previous part of the course, I edited the standard loop to check for archive pages. So I checked for the archive page or the main blog page. Now this time, I'm gonna do it slightly differently. I'm gonna create an extra loop file. And the reason I'm going to do that is because I already have the template files in my theme that will be referencing that loop file. And because if I create another loop file, I can make that file nice and clean and I don't have too many conditional tags all over my main loop.php file. And the third reason is it shows you an alternative method of doing it. So let's start by creating that file, and I'm gonna do so by duplicating our loop.php. So I'm gonna call that loop, -single.php. So I'll edit that, and I'm gonna put a note at the top. Just remind me what it does. So let's take a look at some of the things that we did before. We don't need this check for the archive or home. But we do need the content. So we'll take all of that out. Just do a little bit of cleaning up and tidying and editing. And I'm gonna do the same thing here. Hang on a moment. Let me just undo that. So I'll move this class="entry-utility"> back outside my conditional tag, Of my comment form and then closing PHP tags there. And then section underneath it which is as it was before I started editing the standard loop. So we're now back to something that looks a little bit like the loop file, in fact, it looks exactly the same as the loop file that we have at the beginning of the course. But I'm gonna make some changes to it, because it's for single posts and pages. Now one of those we'll check whether we're in the page. And one of those, we'll apply in all cases. Now the first one is because we're on a single postal page, we don't need and we really don't want a link to that postal page. Because it's extra HTML, it'll confuse people and it's a mess. So let's take out this a. Okay, so we now have our h2 class = entry-title. So that's the first one that applies to both posts and pages. The second change is on our entry-meta here. And what we need to do here is check whether we're on a post but not a page. Because on a static page, you don't really want to echo out the date that a post was written on. So let's add some conditional tags here. So if we're on the page, Cut and paste that there, echo out section class="entry-meta". Don't need the php there, we'll just do get_the_date. And then we echo that one out as well. At the moment, this will happen if we're in a page. So we want to add that exclamation point to say if we're not on a page. Now the reason I did a negative check for pages is so I don't end up getting confused by posts of custom post types and so forth. So we'll just go for is page. So we've made two changes here to our single loop. One of them is for just pages, it won't show the date, and the other is for everything we've taken out that link, so I will save that. But in the moment that wont actually do anything in our site because that file isn't called by any other files. So let's take a look at template files where we'll be calling that loop single file. But the first one is in page.php. So here, you can see we've got get_template_part( "includes/loop');. Now what we do here is add an extra parameter, and that's single. And that will call include/loop- single. And the reason we used two parameters here is because it will default to the standard loop if that single file isn't there. If we put in includes/loop- single, it wouldn't work if that file disappeared. So we'll save that one as well and then we'll do the same in all single.php. So let's save those files and go back and test it on our site. So I've opened up a single post and there's an error here, which WordPress is telling me about, so I need to look in loop-single.php on line 15. Let's take a look, loop-single.php line 15, yes, I've missed out my semicolon at the end of that line there. So save that, and then go back and check. So I've refresh my post, and you can see, that here, there's no link, and then if I go into the about page which is a static page, again, there's no link, and there's no date. So that customizes our loop for our static pages and our posts. So that's how you create a loop file for your single content instead of your archives. In the next part of the course, we'll move onto looking at custom loops. And the first thing I'll show you how to do is create a custom template file in your theme, which will customize the loop. See you next time and thanks for watching.

Back to the top