Viewers: in countries Watching now:
At this point, we've styled our index pages and all our archive pages to match our designs. That leaves us with two more index type pages, but they're not index pages, they're a little bit different. The first one is the search.php template, which handles search results, and the other one is a 404.php template, which handles situations where there, the link points to nowhere. So let's first take a look at what these templates output out of the box. I'm going to start with the search.
So I'll go and search for something I know is there. So I'll search for post, and see what happens. And here you see because of our dry development practices, and use of existing structures within WordPress, our search results page actually looks fine out of the box. Here we have the page header that we've styled in the previous movie, and it tells us search results for, and then the search entry. And then you see the actual search results, and if we scroll down to the bottom, you'll see we have the paging navigation. So the search results, when there are results, works just fine.
However, if I go back and make another search, and I search for nothing, so I'll just put in some random stuff here. We got a different result, and that's because our search template has a fallback for when you don't find a search, and we clearly have to do something about that fall back. In fact, this is what you normally see when you get to a search page on a WordPress site, that returns nothing, and it's not very helpful. It says, sorry but nothing matched your search terms, please try again with some different keywords and gives you the search form.
But that in itself, isn't really enough, or at least I don't feel it's enough. This exact experience is also duplicated, if you go ahead and put a garbage URL in the address field. You get again a message, oops, that page can't be found. And then you get a further message, it looks like nothing was found at this location, maybe try one of the links below or search, and then you get a list of the most recent posts. And most used categories and these are actually all widgets. You also don't get the sidebar, which is kind of odd, because the sidebar would actually be helpful in this circumstance.
So here's what I want to do. I'll consolidate the 404 template with the search template, when it can't find anything. And this message here will be far more informative, it'll tell you exactly what happened, and then it'll show you just like it does in other archive page. A proper list of the six most recent posts from the blog, so that people can either do another search with a search form, or they can simply pick one of the most recent posts. To make this happen, we have to make some changes to our existing content, so let's first understand exactly what's happening here.
I'll go and look at search.php, and here you see we have the standard loop again that displays the header first, and then it loops through all the posts. And if we have the posts, it goes and gets the template part called content, and if we have a custom template part that's called content search. But when we don't have the search part, it'll just default back to content. Right here, which is why our search results currently match the index pages exactly. Once the loop is done, it also outputs the regular paging nav, which we styled previously in the course.
And if it has no posts, it outputs the template part content none. And here is content-none. And this is the file we're working. So if we go and look at content-none, you'll see here the page-header starts by saying Nothing Found. Then we have the page-content, and the page-content displays different text depending on what type of error you encountered. So either it says, ready to publish your first post, if this is a blog that has no posts, or, if it's a search that returned nothing, it says, sorry, but nothing matched your search terms, et cetera, and gives you the search form.
Or in other cases it says, it seems we can't find what you're looking for, et cetera, et cetera. So what I want to do here is replace all of this content, and then when I get it to work the way I want it to I'm also going to loop content-none into 404.php. So we're using the same template for two different things. Like I said, I actually want to rewrite this entire template. So here in my code-snippets, I have a completely new template that you can copy out, and paste into content-none. So let's first do that, and then I'll explain exactly what happens here.
So, I'll just highlight everything in content-none, delete it, and paste in my new template. And this dramatically changes the behavior of, content-none. First off, we start out by testing whether or not this is the 404 template, or the search template. If it's the 404 template, then we attach the error-404 class to the section at the top. If it's the search template or another template, then we attached the, no-results class, to the section at the top. Then we display the index body and the entry-header, and in the entry-header, we changed the entry title, depending on what type of page this is.
If it's the 404 page, meaning the URL itself is broken, then we say, page not available. If it's a search result that returns nothing, then we say, nothing found for, and then the search result in an italicised format. And if it's another situation, then we just say nothing found. Then, I'm adding an entry-content section, so it will be styled exactly like a regular post. And within the entry-content section, I displayed different messages depending on the scenario. So either you get a very explanatory message for the 404 page, or you get one for the search page, or we have a fallback for other situations.
Then when you scroll down, you get something new. Here I've created a new loop that calls for the six latest posts. So, here we have an array that calls for post-per-page equals 6, and then I run a new wp_Query that basically goes back to the database and says, gives me the six most recent posts. I create a new loop based on my query, that loops through each of those posts. And here I'm using the same function as before, get_template_part content. So these will now be output just like they are in all the other archive pages.
And finally, I've reset my post data. So now when I save content-none, go back to my site and perform another search that has an error in it. You'll see the output will be dramatically different. It starts off with a bold message saying nothing found for your search term, and then it said nothing matched your search term. Check out the most recent articles below or try searching for something else. Here we have a search form, and we have most recent posts, and here we have the six most recent posts. So now that I know it works, I need to get the 404 template to do the same thing.
So I'm going to go back to my theme, and open 404.php. And here you see right now, the 404 template is outputting all of this content for me. So I'm going to replace everything from this section and down. So I'll select section, and then scroll down all the way until I get to the end for section. And in place of all of that, I'm simply going to call on content-none. So, I'll say php get_template_part content, none and end my PHP delimiter.
Because this is the 404 template, so we'll never display any real content, and I can simply just call on this template right away. I'll save 404.php. Go back to my site, and enter a garbage url. And now we get a customized 404 message saying, page not available, which is the error. You seem to be lost, to find what you're looking for check out the most recent articles below, or try a search. There we have our most recent posts, and everything works okay except we're missing our sidebar. So I can go back into 404.php, and call in our sidebar, so I'm going to put in a PHP delimiter, get_sidebar.
And, close it, and reload my page, one last time. And, now we have a completely custom experience, no matter what the user does on the site. If you put in a search, you get a search result. If the search gives no results, you get a new search form that tells you exactly what went wrong, and gives you options to view the most recent posts. And if you somehow follow a link that points to nowhere, you get a clear message saying the page is not available, that also gives you a search form to find what you're looking for, and a list of the most recent posts.
There are currently no FAQs about WordPress: Building Themes from Scratch Using Underscores.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.