Content-Specific Standard WordPress Templates - dummies

Content-Specific Standard WordPress Templates

Four content-specific WordPress template types are available: author, category, page, and tag. Most WordPress template files span a wide scope of site views specific to the view, not the content. For example, the category.php template file applies to all category archive views but not to a specific category, and the page.php template file applies to all page views but not to a specific page.

However, you can create template files for specific content and not just the view. Each content-specific template allows you to refer to specific content by the term’s ID (an individual author’s ID, for instance) or by the slug.

For this section, slug refers to a post, page, category, or so on — for example, a Press Releases category having a press-releases slug or a Hello World post with a hello-world slug.

Imagine that you have an About Us page with a 138 id and an about-us slug. You can create a template for this specific page by creating either a page-138.php or a page-about-us.php file. In the same way, if you want to create a template specific to an awesome author named Lisa with a 7 id and a lisa slug, you can create an author-7.php or an author-lisa.php file.

Creating a template using the slug can be extremely helpful for creating templates for sites that you don’t manage. If you want to share a theme that you created, you can create a category-featured.php template. This template would automatically apply to any category view that has a featured slug.

Using categories as the example, the filenaming conventions are as follows:

  • A template with the filename category.php is a catch-all (default) for the display for all categories. (Alternatively, a template with the archives.php filename is used to display categories if a category.php does not exist.)

  • You can add a dash and the category ID number to the end of the filename to specify a template for an individual category.

    Alternatively, you can add a dash and the category slug to the end of the filename, as shown in Table 12-6, to define it as a template for that particular category. For example, if you have a Books category, the category slug is books; the individual category template file is category-books.php.

  • If you don’t have a category.php, an archives.php, or a category-#.php file, the category display pulls from the Main Index template (index.php).

Category Template Filenaming Conventions
If the Category ID or Slug Is . . . The Category Template Filename Is . . .
1 category-1.php
2 category-2.php
3 category-3.php
books category-books.php
movies category-movies.php
music category-music.php

Because creating a template using slugs is so useful (and because an id is only relevant to a specific site), you may wonder why the id option exists. The short answer is that the id option existed before the slug option; however, it is still valuable in specific instances.

You can use the id option for a content-specific template without worrying about the customization breaking when the slug changes. This is especially helpful if you set up the site for someone and can’t trust that he’ll leave the slugs alone (such as a category with a news slug changing to press-releases).