• Aucun résultat trouvé

Creating an archives page template

Dans le document WordPress 2.8 Themes Cookbook (Page 138-144)

After learning how to create a simple page template in the recipe Creating a simple page template, you're probably brimming with ideas for custom page templates that you can provide for your theme. However, if you're going to take the time to create any page templates at all, you should make sure that you provide your users with a useful Archives template.

The Archives template can contain many things, but its main purpose is to help your users navigate around your blog in a way that makes sense to them. As such, it should almost always include a post archive by month, and a list of the categories on your blog. In this recipe, we'll be providing just that.

Getting ready

To properly use the techniques in this recipe, you'll need to be working with a theme that you previously acquired or developed. If you haven't started developing a custom theme yet, I recommend using the Thematic theme. It can be freely downloaded from the WordPress.org Theme Repository at http://wordpress.org/extend/themes/thematic/.

How to do it...

First, follow the steps in the recipe Creating a simple page template until you reach the point at which you start adding custom content. While following that recipe, modify the filename from hello-world-page-template.php to archives-page-template.php and change the value of the Template Name: header from Hello World to Archives. Now, you're ready to start adding the appropriate content. After the page template comment header, add the following markup to your page template, and then save the file:

<?php get_header(); ?>

<div id="container">

<div id="content">

<h2>Archives by Month</h2>

<ul>

<?php

wp_get_archives(array(

'type'=>'monthly', 'show_post_count'=>true ));

?>

</ul>

<h2>Archives by Category</h2>

<ul>

<?php

wp_list_categories(array(

'title_li'=>'', 'show_count'=>true ));

At this point, your Archives page template is ready for use. Go and create a new page in the WordPress administrative area and make sure that it uses the Archives page template. If you need more information on how to do this, see the recipe Creating a simple page template.

Visit your newly-created page. You should see output similar to the example shown in the following screenshot:

Here you can clearly see the month and category archives produced via your custom Archives page template.

How it works...

To learn more about the ways in which WordPress stores and displays custom page templates, see the How it works... section in the recipe Creating a simple page template.

Here, you're using two new functions that are particularly valuable in an Archives page template.

The functions are wp_get_archives and wp_list_categories. Both of these functions are great because they:

Automatically produce a list of sorted links

There's more...

Both of the new functions that you are using, wp_get_archives and wp_list_categories, take a variety of parameters that can be used to modify their output. Let's look at some of these parameters in detail.

Listing archive links

wp_get_archives supports a wide variety of parameters that greatly change the way that the output is produced. The most important parameter is definitely type, as this completely modifies the output by providing a different level of granularity for the archive.

The values available to be used for type are:

monthly yearly daily weekly postbypost alpha

Each of the time-based values for type produces a list that contains an item for each of those timeframes that contains a post. For example, you can see the output for a weekly list as follows:

On the other hand, postbypost and alpha produce a list of each post on the blog, sorted alphabetically. You can see a partial list in the following screenshot:

For a full list of parameters supported by wp_get_archive, visit the WordPress Codex page for the function, at http://codex.wordpress.org/Template_Tags/wp_get_archives. Listing Categories

One of the best ways to browse a site's archive and really find what you want is to follow the categories that interest you. The wp_list_categories function makes it easy for you, as a theme developer, to provide this capability. The wide array of parameters that wp_list_

Some of the most used parameters for wp_list_categories are number, show_count, and child_of. The number parameter limits the number of items output in the category list. The show_count parameter is a Boolean value that determines whether the number of posts in a particular category should be output as a part of the list items produced for that category. Finally, the child_of parameter indicates which categories should be retrieved and displayed based on their parent category. If you wanted to display four child categories of the category with ID 3 and show the number of posts in each category, you'd use something like the following code:

<ul>

<?php

wp_list_categories(array(

'title_li'=>'', 'show_count'=>true, 'number'=>4,

'child_of'=>3 ));

?>

</ul>

This code would display output very similar to the example shown in the following screenshot:

For a full list of parameters supported by wp_list_categories, visit the WordPress Codex page for the function at http://codex.wordpress.org/Template_Tags/

wp_get_archives.

See also

Creating a simple page template

Dans le document WordPress 2.8 Themes Cookbook (Page 138-144)