Creating and using a custom template

WordPress allows you to create custom templates. These can be used only for pages, and not posts. In Chapter 4 we discussed applying the one custom template called Archives that comes with WordPress to one of the pages in the site. Now that we've moved from the default theme to this new custom theme, we no longer have that template. Let's create an archives template for this new theme.

If you click on Blog Archives in the page header's main navigation menu, you'll see that it's displaying only the page content:

Categories

Food Resources Blog Archives

Recipes Found

Uncategorized Let's experiment with the archives template.

Categories

Food Resources Blog Archives

Recipes Found

Uncategorized Let's experiment with the archives template.

Archives November 20Q8

It really should display a complete list of categories, monthly archives, and tags in use on the site. To do this, we need to create a template. These are the steps we'll take:

1. Create the template file by copying an existing file in the custom theme.

2. Add WordPress functions to the template file.

3. Tell the Blog Archives page to use the custom template file instead of page.php.

Let's get started.

1. Create the template file.

Make a copy of page.php within your theme and give it a new name. I like to prepend all of my custom template files with tmpl_ so that they are sorted separately from all the WordPress template files that I will create. I'll call this file tmpl_archives.php.

In order for WordPress to be able to identify this file as a template file, we need to add a specially styled comment to the top of the page (just as we did with style.css). The comment needs to be formatted like this:

Template Name: Blog Archives

In the WP Admin panel the template will be identified by this template name, so make sure the name signals to you what the template is for.

2. Add WordPress functions.

Edit your new template file and remove the loop entirely. That is, remove it from <?php if (have_posts()) : ?> to <?php endif; ?>, and everything in between. Instead of the loop, we'll add some WordPress functions that will display what we want. Because we are creating a custom template, we can add any of the WordPress functions we discovered earlier in the chapter as well as any other WordPress functions (see Chapter 11).

First, let's add a complete list of categories and monthly archives. In the spot where the loop was present, insert this code:

<?php wp_list_categories('title_li='); ?> </ul>

This should look familiar — I copied and pasted it directly from sidebar.php.

3. Apply the template to a page.

Leave your HTML editor and log in to your WP Admin. You need to edit or create the page in which you want to use this template. In this case, I already created the required page and so I'll edit Blog Archives.

On the Edit Page page, look for the Template menu within the Attributes box (at the right, by default).

Attributes

Main. Page (no parent) H

You can. arrange your pages in hierarchies, f example you could have an "About" page th-has "Life Story" and "My Dog" pages under it There are no limits to how deeply nested yoi can make pages.

Template

I Defautt Template! H

Default Temptate litoi'i templates you ca that might have additional features or custom layouts. It so, you'll see them above.

Change it from Default Template to Blog Archives and click on Update Page. (Note that you can also change a page's template using Quick Edit on the Pages | Edit page). Now when you return to the frontend of your website and re-load the Blog Archives page, you'll see that the categories and monthly archives are listed:

Categories

• Food Resources

• Recipes Found

• Uncategorized

Archives

• November ZOOS

Categories

• Food Resources

• Recipes Found

• Uncategorized

Archives

• November ZOOS

To make this a bit more exciting, let's add one more WordPress function to tmpl_archives.php. Underneath the monthly list, add this code:

This function prints all of the tags in use on the site, one after the other (inline, not in a list format), and increases the font size of the tags that have been used more often. Save the template file and re-load the Blog Archives page to see the tag cloud (not so impressive right now as I have so few posts and tags in use), shown as follows:

There is no limit to the number of custom templates you can make in your WordPress theme.

Blogging For Business

Blogging For Business

Download this Guide And Discover How Start And Run Your Very Own Successful Business Blog. Always Wanted To Start Your Own Blog?

Get My Free Ebook


Post a comment