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

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.1 Coding the Loop in Archive Pages

Hello, and welcome back to this Tuts+ course on working with the WordPress Loop. In this part of the course, I'm gonna show you how to code the loop in an archive page. So in other words, in a template that is designed to display an archived posts. So you can find those in various places on your site. So here, for example, on our front page, this is a static page, so there's no archive on here. But on your homepage of your website, you might have all your blog posts. I've got my main archive page for my new posts on my blog page here, rather than on my homepage. So that is a kind of archive page. It's a special archive page because it's the main blog page. And you can see at the moment, it's set up to display every post, all of the last ten posts on each page, and it's displaying the whole post. And we're gonna edit that when I show you how to code the loop. Now, let's go a bit further down and see if I can find. Here we are, here's some tags. So I'm gonna click on this tag, and you can see that is yet another archive page. And again, it's displaying the whole post. So I'm going to edit this in my template file in this part of the course. So let's take a look at the code. Now, there are a number of files you could use to create your archive pages. And you can see here the obvious one is archive.php. Now that will work for every archive page except the main blog page. Because that is the homepage in WordPress terms, and that's controlled by another file. Now let me show you this using the WordPress template hierarchy. So here we have the template hierarchy. And you can see here in an archive page, depending on what kind of archive it is, you can create a custom template for that kind of archive. So you could create an author.php template file for all of your author archives, or you could get more specific and create ones for each of your authors. Or just one of your authors if you want them to have a slightly different template. But I'm gonna get less specific here and use the archive.php file. And that will work for an author archive, category archive, custom post type, taxonomy data archive, or tag archive. But what it does not work for is the main post archive because that is our blog post. So that is either shown by index.php, which is the catch-all template file that can display any kind of content, or by home.php. And the only two options you've got here for your main blog posts page is home.php or index.php. And in a moment I'm gonna create a home.php file to show you how that works, but first let's go back to our archive.php file. So here's my file, and instead of having the loop coded into it, you'll see, as I showed you in previous parts of the course, that it uses get_template_part to fetch an include file. And that makes your coding more efficient because it means you only need to code the loop once. So let's take a look at that include file. We've got loop.php here. Now, I have two options here. I could either create an alternative loop-archive.php, which I then call here instead of just loop, or I could put some conditional code inside my loop file. And that's what I'm going to do here because all I want to change is this line here. So all I'm changing here is the content. So in my section class = "entry-content", I'm going to add a conditional statement. So my conditional statement, my conditional tag is, if is_archive. Now, in here, I could use a parameter to specify what kind of archive. But I'm not gonna do that because I want to have any kind of archive on here. So I'll just create some space here and then I'm going to put the_excerpt. And then if that isn't the case, if we're not in an archive, I'll use an else, And then move the content up here. So what I'm doing here is I'm telling WordPress if we're on an archive page, show the exit or if we're not show the content. So I'll save that, and then I'll go back to my archive page and see what's happened. So you can see here, I've got two posts on on this tag, and instead of it being a whole load of content, we've just got an excerpt here. Now, I don't want to include the comments, so let's go back and have a look at that code, and again, I'm gonna use this conditional tag. But I want it to be not an archive, so I use an exclamation point there to say if it's not an archive, we show the comment form. I'll get rid of the php there. Close my braces, and my conditional tag and then that's my entry utility. Now at the moment that's not ideal, because this section class entry-utility will open up whether or not I'm in an archive. But I might want to keep that because I might want to add some other code in here later on. Now, that's not actually the case, so I'm gonna change that a bit. And I'm gonna move this up, php is_archive, and I'm gonna do echo section class = "entry-utility", comment_form and then, echo section. So I can see there that I'm closing my entry-utility section. So again I'm closing my brackets there and doing that. And another way to do it is to alternatively have a conditional tag that goes around both these two sections, entry-utility and entry-content. And which way you do it is entirely up to you. I'm gonna keep them separate in case I want to add anything extra in either these entry-content or these entry utility-sections. So I'll save that, and then I'll go back to my tag archive, and you can see all I've got here is Get Started with WordPress Code and Theme Development- posts and tutorials. Now I'd quite like to add a link here to say Read More. So under the excerpt here, I'm gonna echo out a link to the post, with the text Read More. So that will echo out a link to the permalink with Read More, which will be internationalized if needs be. So let's save that and take a look at it on our site. So now you can see there's a Read More link underneath. People can already click on the title, but that gives them somewhere else to go. So that's how you create a custom version of the loop right within your loop file that works in an archive page. Now, let's take a look at the homepage. So the homepage doesn't use this archive, it uses the index.php. And again, that's fetching the loop. So because I'm referencing the same loop and all I'm changing is my loop, I don't need to create a new home.php file, I'll just edit the loop. So here, I need to change my conditional statement. And let's take a look at the Codex and what it gives us for conditional tags. So there are three conditional tags that can apply to our homepage. And the term homepage in WordPress is a bit confusing because home is the main blog page. And that doesn't matter whether that's actually the front page of your site, or the blog page as I've got to set up if your front page is static. Now in our case, all we want to check for is that we're on the main blog page. And it doesn't matter if that's the homepage or the front page of the website as well. So we just use is_home. So back in our loop file, we want to add another check in our conditional query. So we want to know if this is an archive or is the homepage. And then here we want it to not be an archive and not be the homepage. So if we're either in the archive or home, it'll show the excerpt, and if we're in neither the archive nor home, it'll show the comment form. So I'll save that, go back to my site's blog page, and you can see on the main blog page, we've now got just the excerpt. Now, let me show you that this will work on the homepage if that's also the blog page by using the Customizer. So here on Homepage Settings, let's change that to latest posts. I'll click on the link to the homepage and you can see that that works in exactly the same way. So now you can see how to change your loop in any archive page. So that could be category archive, a tag archive, and also your main blog post archive. Now if you want to get more specific with that, and just hone in on a category, you can either create a category.php file, or you could use a conditional tag for a category. Instead of, here you see it's there, instead of just for an archive. But using is_archive will cover any kind of archive page apart from that homepage. In the next part of the course, I'm gonna show you how to customize the loop for single posts or pages. See you next time, and thanks for watching.

Back to the top