Justin Tadlock

Excerpts and taxonomies for pages in WordPress 2.9

While the information in this tutorial will be focused on excerpts and taxonomies, the big change in WordPress 2.9 is that the meta box functions have been included in a new file: /wp-admin/include/meta-boxes.php. This change allows us to do some fun things because the code is not limited to a single area anymore.

In this tutorial, you’ll learn how to utilize the add_meta_box() functionality to add two things to your pages:

  1. Excerpt box.
  2. Custom taxonomies boxes.

Being able to add these meta boxes to pages isn’t the limit. This tutorial should serve as a starting point. You could do the same for custom post types, links, or anything that fires the do_meta_boxes action hook.

Adding an excerpt box to your page editor

The first thing you should do is open your theme’s functions.php file in your favorite text editor. Paste the following PHP code into it:

add_action( 'admin_menu', 'my_page_excerpt_meta_box' );

function my_page_excerpt_meta_box() {
    add_meta_box( 'postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core' );
}

Now, you’ll be able to add excerpts to your pages, just like posts. This can come in handy with themes that display search results in excerpt form.

Adding custom taxonomies to your page editor

This one will be a bit trickier. I’ll have to assume you’ve created a custom taxonomy specifically for pages. If you’re unfamiliar with this process, you need to familiarize yourself with creating custom taxonomies.

Let’s assume you’ve created a “People” taxonomy for pages. Your custom taxonomy code should look a little like this.

register_taxonomy( 'people', 'page', array( 'hierarchical' => false, 'label' => 'People', 'query_var' => true, 'rewrite' => true ) );

The important thing in that line is page. We’ll be adding meta boxes for all taxonomies that have been registered for pages.

Again, we’ll go to our theme’s functions.php file. Paste this PHP code in:

add_action( 'admin_menu', 'my_page_taxonomy_meta_boxes' );

function my_page_taxonomy_meta_boxes() {
    foreach ( get_object_taxonomies( 'page' ) as $tax_name ) {
        if ( !is_taxonomy_hierarchical( $tax_name ) ) {
            $tax = get_taxonomy( $tax_name );
            add_meta_box( "tagsdiv-{$tax_name}", $tax->label, 'post_tags_meta_box', 'page', 'side', 'core' );
        }
    }
}

Once that’s done, you’ll be able to use your page-based custom taxonomies.

Please note that hierarchical taxonomies aren’t going to be available yet. Maybe in WordPress 3.0?

A good move by WordPress

I’m glad WordPress is moving in this direction with some of its functionality. Average Joe might not realize the implications of something as simple as moving code to another file, but developers should rejoice. The reuse of code is a cornerstone of good development practice. By making things more modular, it becomes easier to create new things without rewriting a lot of code.

I’ve shown you how to do these things with pages. Just imagine being able to make these simple and quick changes for custom post types (i.e., content types), essentially having the ability to create any type of site you want with WordPress.