Chapter 7. Tips and tricks

You now have a great working WordPress theme, which uses valid, standards-compliant code and is fit for release to the public. Congratulations!

In this chapter we'll look at some added extras, some additional bells and whistles you can use to make your theme just that bit better.

You'll learn how to create and make use of additional template files to add extra flexibility and functionality to your theme, as well as how to use conditional tags to display different content in different parts of your site. You'll also learn how to make use of the Theme Customizer and optimize your site for SEO.

You can create perfectly good, workable WordPress themes without any of these extras, but you'll find that you can take your WordPress themes much further with these techniques.

So let's get going!

Adding more template files to your theme

In Chapter 3, Coding it Up, we looked at the template files you need for a basic WordPress theme, and we spent some time creating them. We also learned about includes, files which you call from your main template files, such as header.php.

So far we've created the following template files for our theme:

  • Template files:
    • index.php
    • page.php
  • Includes:
    • header.php
    • sidebar.php
    • footer.php
  • The functions file:
    • functions.php
  • The stylesheet:
    • style.css

But let's say our site needed to display posts from a specific category differently from the rest of the site, or we needed the home page to work differently, or maybe we wanted to have more control over how search results or 404 pages were displayed.

With template files, we can do all that.

A search.php file for search results

WordPress handles search results pretty well already. Let's see what's displayed in our theme if we try to search for example post (Note that we've now added a Search widget to the right-hand side footer widget area to make this possible):

A search.php file for search results

As you can see, it' s using our index.php template file, so the heading reads This Month:. We'd rather make it more obvious that these are search results.

Now let's see what happens if we search for something that can't be found:

A search.php file for search results

Again, the heading isn't great. Our theme gives the user a message telling them what's happened (which is coded into index.php as we'll see), but we could beef that up a bit, for example by adding a list of the most recent posts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset