techno, faq post

How to create custom category templates

customcats

This article will show you how to create custom category templates that are automatically used when specific categories are selected from the site menu, via a conditional statement in the category.php file. While it is possible to have categories default to using a custom template named with the category number (i.e. category-12.php), this is a simple, logical technique for displaying specific categories using custom templates, based on a conditional statement in the category.php WordPress template. Another advantage to using this technique over the category/number approach is that you may have multiple categories that can share a single template (i.e., “products” and “books” might use the same layout) so you can avoid creating extra documents.

How it works

If a site visitor clicks on “books” she will be served a unique layout, directed by the category.php file in the site theme, that grabs all of the posts in the “books” category and presents them in a specific way. If the visitor clicks on “tutorials”, that same category.php file will grab a different template to present the posts in the “tutorials” category. When rolled over in the site menu, the browser status bar (bottom left of your browser window in Firefox) would show the link to these “pages” : http://domainname.com/category/books. The link in your theme template would be : /category/books. Same thing for the menu link to the tutorials category.

How to set it up

For the sake of this example, we will assume that you have three categories in your WordPress installation – News, Books and Tutorials. We want a custom template for the Books and Tutorials categories and we want the News category to use the default category template. If your current theme doesn’t have a category.php template, you can grab it from the WordPress default theme.

  1. Make four copies of category.php and name one of them something like “category-original.php” just in case you want the original back later on down the road.
  2. Rename the other three copied files category.php, category-default.php, category-books.php and category-tutorials.php. The hyphenated files will be your new templates – you can actually name them anything you like; I just prefer this naming convention because I think it’s easy to understand and the files show up together when viewed alphabetically. Please note that these templates do not require the opening php “template tags” that Worpress uses for custom Page templates; those are only for Pages where you select which page template you want to use. All the work here is done by category.php, which decides which category template to use. I agree that using the word “template” in so many different ways could be confusing…
  3. Edit each hyphenated template as necessary to present the content intended for it.
  4. Now, in category.php, delete everything and replace it with this  (you’ll need to remove the extra space after the beginning of the php tag question mark) :
    <? php
    $post = $wp_query->post;
    if (in_category('books')) {
    include(TEMPLATEPATH . '/category-books.php');
    } else {
    if (in_category('tutorials')) {
    include(TEMPLATEPATH . '/category-tutorials.php');
    } else {
    include(TEMPLATEPATH . '/category-default.php');
    }
    }
    ?>

What this script tells the server is :
- If the link to the books category has been clicked, use the template category-books.php to present data from that category
- Else if the link to the tutorials category has been clicked, grab category-tutorials.php to present that data
- Otherwise, use  category-default.php to present the data from whatever category has been selected for viewing

Extras

A similar technique can be applied to the single.php template and you can also employ conditional statements to doing neat things like changing header graphics or page layout based on the page or category being served. More on that to come in another article…

If you are using a wonderfully handy plugin like Improved Include, you can also include a page – like intro text – at the beginning of the custom template rather than adding that content to the template itself. This is a good way of ensuring that you keep the content and structure separate, and enabling changes to the intro text to be conveniently made in the WordPress admin area. If you are using this plugin, you would simply add the following directly before if (have_posts)) :

<? php if(function_exists('iinclude_page')) iinclude_page('6','displayTitle=true');//use your page number ?>

Laissez-nous une réponse.
Leave a comment.

RechercherSeek

TopicsCategories