3.10 The Single Page and Post Templates
In this lesson we'll build the single.php and page.php, which are responsible for displaying a single post and a single page.
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.10 The Single Page and Post Templates
Hey. Welcome back. In the previous lesson, I finished the index.php, which was spread out across multiple lessons. Currently, index php is used to display all the pages, including standalone pages like this lorem ipsom that you see right here. But here's the thing, when displaying pages, you would want to use a separate template, and that's what page.php is for. So if we create page.php it's a blank page. Now if we refresh, notice we don't have anything on that. If we go to the homepage we can see that index php is being used but if we check out a single page there's nothing. So let's write that. We start with the header of course, so php get_header. Refresh and there's our header. Next we're going to have the content. Now the content will be placed in a DIV with a class of "main content" and a COL, "md-8". Role, and it's going to be "main". So just like index.php yeah. This is very similar. Next, we're gonna load the sidebar and the footer. Okay. So now we have everything we need in the page except the content. Now, on the content, since this is a stand alone page, it uses a separate loop for displaying the content. So we're gonna start with this php while ( have_posts() ) and we're gonna load the_post. And we don't do the traditional check that we did on index.php, yeah, if we have posts because if we're landing on this page here, then we all ready have a post, so that check is unnecessary. So while we have posts, we load the posts. And I'm gonna do an end while here so I don't forget, and then I'm gonna start with an article with an ID of post dash, and then php I'm gonna put the ID in here. And then I'm gonna say another php post_class(). You might be familiar with this because it has been used in content php. It's basically the same thing. And in fact this is very similar to the content php sum. I'm going to grab this stuff right here and I'm going to paste it inside my article. And then, just keep only what I need. Let's clean up this up a little bit. So, what we need, we need the post thumbnail because that can also be displayed on pages. But the only difference is, the title will be displayed right here, after the post thumbnail because we don't have to do a check if it's single anymore, ok, so, I'll delete that. Next we don't need the entry meta anymore, so delete that as well. Next is the entry content, where we're just gonna have. The content has some. The content and then php link pages. Because pages can also be split up into multiple entities and what else we have the entry footer. Which should only contain the edit link, right? It's a page you still need to edit that, and for that we're going to go to our functions php and I'm going to grab this bit right here. Yeah, so if the user is logged in, then we're gonna display the edit_post_link. The only difference is I'm not gonna display it as a list item anymore. Instead, I'm gonna use a paragraph. So paragraph here, and close it here, okay? So, let's refresh, and there is the content. And right at the bottom, you can see the edit link because I am logged in. Now, the last thing we gotta do here after the article is closed, I'm gonna do this. Php comments template. It's a page so it should have comments. And comments template, if we save now, you're gonna notice it needs comments.php which isn't created yet. So let's go ahead and do that, comments.php. Yeah, and I'm just gonna leave it like this for now. Refresh, and now nothing is shown yet. Okay, so that is the template for page.php. Now, what happens when you click on a post? For example, Template: Sticky, right? This is a post. It currently uses index.php to render, but if we do single.php, it will be overwritten because single.php. Should only be used to control how you display a single post. So, let's write this real quick. >. Okay, this is very similar to page php. So, I'm gonna copy this content. Yeah? We need the header. We need the side bar and the footer. But the content is a bit different. So we still need the main content and col-md-8. Instead of this whole content that you see right here, I'm gonna say php get_template_part( 'content' ). And then get post format and then comments template and this will ensure that for every post, the correct template will be loaded from this. We have content, audio content, gallery, image, just like we did in index.php. The only difference is this is the single post page. And it has the comments template. So that's about it for this lesson. We've created page php, single php. And now the only thing that we need to do to complete. This area of the framework is the commons template. That's coming up in the next lesson.