3.12 The Author Archives
In this lesson we'll build the page responsible for displaying all posts written by a certain author.
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.12 The Author Archives
Hey, welcome back. In this lesson I am gonna show you how to write the author archive's page. And the author's archives basically shows you a list of posts written by a specific author. You can access it by clicking on the author name inside a post meta. So, for example, author=1 when you click it, we should get a list of posts that are written by that author. So going back here, I'm gonna create a new file called author.php, and this will override the default that you saw here. So right now we have a blank page. Okay, and we start with a php tag as always. And now we start like every other page with the header. So get_header. And then we're gonna have the content which will be a div with a class of main-content.col-md-8 role=main. And then we also have a sidebar and we also have a footer, so get_footer. Now this page we're building here is very similar to, for example, the index page. The only difference is we gotta show some additional information on the top of the page about the author. So that's why we're building a separate page. So, on the top, we first gotta get some author information. And for that, we're gonna start with this. If we have_posts() written by this author, then we're gonna do the_post(). And that will basically queue the first post so it can get the author info out of it. And I'm gonna close the if right here. Okay, inside I'm going to start with a header class of page-header that's going to contain an h1 which will say, printf. We're gonna do a translatable string that says, all posts by %s, and this denotes a string, basically. So all posts by author, alpha here and here we're going to say get_the_author. Now get_the_author is a function that only works inside the loop, and it gets the author name from the current post, from the post that's currently loaded. Yes, okay, so that's the h1. Next, we can also get the author description, so we can say something like this, php, if the author biography exists, display it. So we'll do a simple check here if, get_the author_meta description. Then we're gonna do an echo, paragraph the_author_meta description. And then we're gonna close the paragraph. Okay, next we're gonna do this php rewind_posts. So what this does, because we called the post above, we need to rewind the loop to the very beginning so we can run it properly for the rest of the posts if that makes any sense. So basically we're just starting over. The first post, we loaded that one because we needed information about the author. But now we've got to go to the very beginning to display the other posts. So, below the header, I'm gonna do this php while we have_posts(), so we're continuing the loop. I'm gonna do the_post(), and we're gonna end the while. And then I'm gonna do another php that says get_template_part content, and whoops, get_post_format. So we're loading each post depending on which post format it has. After this is done, we gotta do the paging, so alpha paging_nav, and after that we gotta do this. else, so this is the else for the if that we opened here. So if we don't have any posts, we're simply going to do a php get_template_part contents, none. And then, endif, and that's it basically. And, actually, I already did an endif here. My bad, so without that, let's refresh, and there it is. So as you can see, all posts by admin, this is my biography. And here there's a list of all the posts written by admin. If we search for another author like themedemos, open that up, all posts by themedemos. We don't have a biography for this one, but it does show an entire list of posts written by the same author. So our script is working perfectly. Okay, that's it basically for this lesson. Next time we're gonna take care of the date archives.