Dissection of a WordPress theme: Part 3

Page list

Pages are a new feature in WordPress 1.5 and allow you to define ‘static’ content outside the normal blog timeline. This is very useful for more permanent content such as contact pages and author biographies.

Typically you want your pages to appear in the sidebar so that visitors can navigate to them. This is achieved with:

<?php wp_list_pages('title_li=<h2>' . __('Pages') . '' ); ?>

Notice how this doesn’t look like any of the previous sidebar sections. Full details of the wp_list_pages function can be found in the Codex.

Currently the function is passed the parameter:

'title_li=<h2>' . __('Pages') . '</h2>'

This instructs the function that the title for the page list should be <h2>Pages</h2>. By default the wp_list_pages function returns the pages as an unordered list, matching the rest of the sidebar sections.

Note that another WordPress internationalisation function is used here: __(‘Pages’). This is a function called __(), which does the same as _e(), but instead of echoing the string to the screen it returns it to be used for further processing.

The Codex describes several other arguments that the function accepts, and I will present a few alternatives here.

By default the list will be a multi-level page hierarchy. We can configure the depth as follows:

<?php wp_list_pages('title_li=<h2>'.__('Pages').'&depth=1');?>
Page depth

Left: multi-level, right: single level

We can also display other information along with the page title. Here we show the last modified time of the page:

<?php wp_list_pages('title_li=<h2>'.__('Pages').'&show_date=modified');?>
Page modified

A useful feature is the ability to exclude certain pages:

<?php wp_list_pages('title_li=<h2>'.__('Pages').'&exclude=9');?>

The ID of the excluded page (here shown as 9 for ‘Parent test’) can be found in the WordPress administration page.

Page excluded

Archives

The archive section displays the history of your blog posts. The longer you maintain your blog, the more entries you will have.

<li>
  <h2><?php _e('Archives'); ?></h2>
  <ul><?php wp_get_archives('type=monthly'); ?></ul>
</li>

Like the wp_get_pages function, wp_get_archives also supports several parameters. The example shown above will display the archives on a month-by-month basis, with a maximum of 12 months. A full list of the options can be found in the Codex.

For example, to change the output to show the archives over the last 4 weeks:

<?php wp_get_archives('type=weekly&limit=4’); ?> 
Weekly archives

If you look in header.php you will notice that the wp_get_archives function also gets called here:

<?php wp_get_archives ('type=monthly&format=link'); ?>

This provides a series of links to the archives which, given an appropriate browser, can be presented to the user as navigational aids.

Categories

As you would expect this section displays the categories. These are configured from the administration section and assigned to posts.

<li>
  <h2><?php _e('Categories'); ?></h2>
  <ul><?php list_cats(0, '', 'name', 'asc', '', 1, 0, 1, 1, 1, 1, 0,
  '','','','','') ?></ul>
</li>

The list_cats function accepts many arguments, all of which are detailed in the Codex. There is an equivalent function that accepts arguments as a text string, called wp_list_cats. This is much more succinct and the above function call can be rewritten as:

Just like the other sections, you can modify the appearance of the categories as you see fit.

Meta data

This section includes all the blogroll links, as well as the links to WordPress, XHTML validators, and XFN.

  1. <?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>      
  2.  
  3.   <?php get_links_list(); ?>
  4.  
  5.   <li><h2><?php _e('Meta'); ?></h2>
  6.  
  7.   <ul>
  8.     <?php wp_register(); ?>
  9.     <li><?php wp_loginout(); ?></li>
  10.     <li><a href="http://validator.w3.org/check/referer" title="<?php _e('This page validates as XHTML 1.0 Transitional'); ?>"><?php _e('Valid <abbr title="eXtensible HyperText Markup Language">XHTML'); ?></a></li>
  11.     <li><a href="http://gmpg.org/xfn/"><abbr title="XHTML Friends Network">XFN</abbr></a></li>
  12.     <li><a href="http://wordpress.org/" title="<?php _e('Powered by WordPress, state-of-the-art semantic personal publishing platform.'); ?>">WordPress</a></li>
  13.     <?php wp_meta(); ?>
  14.   </ul>
  15.   </li>
  16. <?php } ?>

The entire section is wrapped inside an if statement (line 1 and 16):

if ( is_home() || is_page() )

The information is only displayed when viewing the home page or a WordPress ‘page’ page. If you want the links displayed regardless of context then remove the if statement and surrounding braces.

The blogroll is displayed with a call to:

<?php get_links_list(); ?>

The only option available with this function is to change the order of links.

Next up is the meta data. This consists of normal links, along with a few special function calls:

  • wp_register – displays the ‘register’ link, or ‘site administration’ if already logged in
  • wp_loginout – displays the ‘login’ or ‘logout’ links
  • wp_meta – enables all meta-related plugins to function

None of the links are mandatory, and you can add your own. I will remove the XFN link as I don’t think it’s necessary.

Additional sidebar functions

As discussed earlier, there are no requirements for what you can and cannot put in the sidebar. Many plugins exist to insert suitable data, and you can find these in the WordPress Plugin Database.

There are also several built-in functions that you may find useful. A full description of all the available functions can be found in the Codex. Here I will present a popular one which displays a calendar, with blog postings highlighted on a day-by-day basis. It’s a nice navigational aid and means a visitor doesn’t have to trawl through archive sections when looking for other recent posts.

I will add the calendar before the meta-data information:

<li>
  <h2><?php _e('Categories'); ?></h2>
  <ul><?php list_cats(0, '', 'name', 'asc', '', 1, 0, 1, 1, 1, 1, 0,
  '','','','','') ?></ul>
</li>
 
<li><h2><?php _e('Calendar') ?></h2><?php get_calendar () ?></li>     
 
<?php if ( is_home() || is_page() ) { ?>

The result is:

Calendar

We will style this in the next section.

Help me to save time by reading these instructions!

If you are asking a question please read the FAQ to see if it has already been answered. All support questions should be directed to the support forum. Thanks!

Comments

Comments are shown on the first page.

Home | Main content | Software | Terms & Conditions | Sitemap | John Godley © 2010