Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Customizing the search results and the 404 template

From: WordPress: Building Themes from Scratch Using Underscores

Video: Customizing the search results and the 404 template

At this point, we've styled our index pages So what I want to do here is replace all of this content, and then when I So, I'll just highlight everything in content-none, Then, I'm adding an entry-content section, so it So now when I save content-none, go back to my You'll see the output will be dramatically different.

Customizing the search results and the 404 template

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.

Show transcript

This video is part of

Expand all | Collapse all
  1. 10m 55s
    1. Welcome
      1m 4s
    2. What to know before you start this course
      2m 40s
    3. How this course and the lesson files work
      3m 12s
    4. Introducing Simone: A preview of the final project
      3m 59s
  2. 21m 28s
    1. Installing and running WordPress on your computer
      3m 53s
    2. Getting and installing Underscores
      4m 11s
    3. Installing WordPress Theme Unit Test data
      4m 18s
    4. Installing the Developer plugin
      2m 58s
    5. Installing and setting up NetBeans or another IDE
      6m 8s
  3. 16m 15s
    1. Designing for mobile, content, and style
      4m 52s
    2. How do WordPress themes work?
      4m 48s
    3. Understanding the WordPress template hierarchy
      2m 19s
    4. Underscores: An overview
      4m 16s
  4. 37m 5s
    1. Setting up style.css
      4m 47s
    2. Configuring baseline settings and functions
      6m 6s
    3. Enabling custom fonts and font icons
      5m 44s
    4. Applying global styles
      5m 11s
    5. Styling basic layout components
      6m 19s
    6. Making the site layout responsive
      8m 58s
  5. 23m 18s
    1. Styling the default header
      6m 25s
    2. Hiding the site title and tagline
      5m 32s
    3. Adding an optional header image function
      5m 23s
    4. Placing the header image behind the site title
      5m 58s
  6. 40m 55s
    1. Setting up menus
      3m 12s
    2. Styling the menu
      7m 42s
    3. Using Superfish for accessible menus
      8m 0s
    4. Making the menu responsive
      7m 3s
    5. Creating a custom social media menu
      5m 51s
    6. Styling the menu with icons from Font Awesome
      9m 7s
  7. 18m 9s
    1. Adding the search form
      6m 27s
    2. Adding the search icon
      6m 55s
    3. Adding show/hide functionality to the search form with jQuery
      4m 47s
  8. 33m 20s
    1. Adding a widgetized area to the footer
      7m 10s
    2. Using the Monster widget plugin to test all widgets
      2m 11s
    3. Styling the footer
      3m 6s
    4. General widget styling
      5m 33s
    5. Adding custom styles to specific widgets
      7m 34s
    6. Using Masonry to make footer widgets responsive
      7m 46s
  9. 54m 49s
    1. Changing the Single Post Template content structure
      5m 54s
    2. Changing the output of meta elements
      7m 2s
    3. Styling the Single Post Template
      7m 57s
    4. Making post meta responsive
      6m 21s
    5. Styling blockquotes
      5m 39s
    6. Creating pull quotes and pull images
      5m 1s
    7. Working with image captions
      4m 27s
    8. Working with image galleries
      4m 57s
    9. Single-post navigation
      7m 31s
  10. 30m 23s
    1. Working with the comments template
      8m 42s
    2. Using Gravatars in comments
      2m 42s
    3. Styling comments
      7m 26s
    4. Highlighting post author comments
      3m 36s
    5. Styling the comment form and messages
      7m 57s
  11. 18m 43s
    1. How do featured images (post thumbnails) work?
      2m 57s
    2. Defining featured image sizes
      3m 30s
    3. Generating new featured images with a plugin
      1m 46s
    4. Adding featured images to a template
      5m 7s
    5. Styling the featured image
      5m 23s
  12. 1h 2m
    1. The index template hierarchy
      2m 21s
    2. Customizing and styling index templates
      10m 10s
    3. Displaying excerpts or full content on index pages
      3m 6s
    4. Adding a custom Read More link
      3m 48s
    5. Adding featured images
      4m 0s
    6. Creating custom pagination navigation
      6m 4s
    7. Highlighting Sticky Posts
      2m 55s
    8. Creating custom post format templates
      5m 30s
    9. Highlighting the most recent post in the index template
      7m 22s
    10. Embracing modular design
      2m 29s
    11. Working with archive.php
      5m 54s
    12. Customizing the search results and the 404 template
      8m 28s
  13. 9m 7s
    1. Styling pages
      3m 4s
    2. Creating custom page templates
      6m 3s
  14. 4m 30s
    1. Adding editor styles to match front-end styles
      4m 30s
  15. 2m 20s
    1. Further learning
      2m 20s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress: Building Themes from Scratch Using Underscores.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.