3.13 The Date, Category and Tag Archives
In this lesson we'll build the date, category and tag archive pages.
1.Introduction1 lesson, 01:20
2.Framework Basics2 lessons, 14:38
3.PHP Files16 lessons, 2:08:48
4.Finishing Touches2 lessons, 23:07
5.Conclusion1 lesson, 01:31
3.13 The Date, Category and Tag Archives
Hey, welcome back. In the previous lesson I wrote the authored up php file. Which is a template for displaying old posts written by a certain author. But now we have some other similar templates that we're gonna make, for the date, category and tag. First of all for, the date I've added a calendar widget right here, so we can see what I'm doing. So if we scroll back to, let's say the 31st of May this year, you're gonna see a single post, Testing, the one that I wrote previously. And you can see in the address it says, m=20140531. But I can go backwards to 2013, for example, in January, let's say, right? And here you can see in the calendar I have a post written on the 5th, which is this markup, yeah, January 5th, 2013. So we need a page to handle this. So we're gonna create a new page called archive.php. And actually, I'm gonna copy the contents from author.php because it's very similar. So we're getting the header, the content stays the same. If we have posts, then there's no need to load the first post, actually. We're gonna have the header, we're gonna have the h1. But inside the h1, things will be a bit different, so we're gonna have a php tag, and we're gonna check which kind of archive it is. Is it daily, monthly, yearly and so on. And depending on that, we're gonna show a different message. So if is_day then we're gonna do printf( _( 'Daily Archives for %s', 'alpha' ), and then get_the_date() );. So this will be displayed as daily archives for the actual date. If it's not a day and it's a month, so, elseif ( is_month() ). And then we're gonna do something similar, printf, and we're gonna say Monthly Archives for get_the_date, but this time we're gonna format the date. So we're gonna use _x, which is another function for creating translatable strings. We're gonna do 'F' 'Y', which stands for the format it's gonna be displayed in. And then a small description, Monthly archives, date format, alpha. Okay, and finally, elseif is year. We're also gonna also have a printf, and we're gonna say Yearly Archives for the date and we're only gonna format it with the 'Y', which stands for the year. And here we're just gonna say, 'Yearly archives date format'. Now else, we're just gonna do this, which is gonna say 'Archives'. Okay? Close the h1, we don't need these two bits, and then the rest is pretty much the same. We get the posts, we get the paging navigation, and if there is no content, we display the proper template. And then sidebar at footer. So let's go ahead and save this. Refresh. And you can see the date is January 5, 2013. So it says daily archives for that date, yeah. If we select something else like January 10, 2013 it says daily archives for that date, and it shows the post written on that date. Yeah, January 10th. And then if we go to March, for example, it says Monthly Archives for March 2012. So we have this, which is written on the 15th and then this, which is also on the 15th. This, which is on the 14th, and so on. And then if we just select the year here, 2012. And it says Yearly Archives for 2012. So, that's basically it for the date archives. Now let's move on to the category archives. So we have a post. And we have a few categories here. When you click on the category, it will show you all the posts within that category. So right now it uses the default template. You can see Archives here. So it uses the default template archive.php, and it shows this bit. So lets go ahead and copy this, all of it, create a new file that's called category.php, paste that in, bit of change there. And then If we have posts, we have the header, or the class of page header, and then inside the h1, I am simply going to do this printf. And we're gonna say Category Archives for %s alpha. And now we've got to replace this %s with the category title. And for that, we're going to use the single_cat_title function, which receives the first parameter is the prefix or the text to output before the title. In my case, it's nothing. And the second parameter Is called display, and it tells us if the page should be displayed or returned to using php. So we're gonna say false, because we're already printing it. So no need to print it again. Close that and then let's show a category description, if there is any. So I'm going to do php, Show on optional category description. I'm going to say if (category_description). So if one exists, we're gonna echo it inside a paragraph, okay. And that's the end of the header. Next we have normal workflow. We did the loop, we get the posts, we display the paging navigation, and if there are no posts, we display the content none. So save that, refresh and now you can see Category Archives for Template. And you can go even further, if you want, and you can add some quotes here, like this. Yeah, and now the keyword is in quotes. But, that's really up to you, okay? And finally, I'm gonnas copy the same content here, create a new file called tag.php, paste it in, I'm gonna change the name here and here. And then I'm gonna do printf, Tag Archives for single_tag_title. It has the exact form as the signle_cat_title. And then show an optional tag description. Tag description here, and the rest is the same. So let's actually click on a tag, Codex, for example. And you can see Tag Archives for Codex. And basically, we are finished with the archive pages with it for the date, for the category, for the tag, and for the author. Now in the next lesson I'll show you how to build the 404 page and the search page. So I'll see you there.