Custom Post Type Index Page in WordPress 3.1 Beta

December 4th, 2010

This will be well-known in a few days I am sure, however if you are looking to add an index page to a custom post type in WordPress 3.1 Beta there are very few documents out there right now.

First of all you will need to know what to name the file so. They settled on archive-{postType}.php as it matches the way a single page is handled. For work you would have:
archive-work.php
single-work.php

The next step is to add ‘has_archive’ => true in your arg variable.

Here is my code for the work section
[sourcecode language=”php”]
add_action(‘init’, ‘my_custom_init’);
function my_custom_init()
{
$labels = array(
‘name’ => _x(‘work’, ‘post type general name’),
‘singular_name’ => _x(‘work’, ‘post type singular name’),
‘add_new’ => _x(‘Add New’, ‘work’),
‘add_new_item’ => __(‘Add New work’),
‘edit_item’ => __(‘Edit work’),
‘new_item’ => __(‘New work’),
‘view_item’ => __(‘View work’),
‘search_items’ => __(‘Search work’),
‘not_found’ => __(‘No work found’),
‘not_found_in_trash’ => __(‘No work found in Trash’),
‘parent_item_colon’ => ”,
‘menu_name’ => ‘Work’ );

$args = array(
‘labels’ => $labels,
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘query_var’ => true,
‘rewrite’ => true,
‘capability_type’ => ‘post’,
‘has_archive’ => true,
‘hierarchical’ => false,
‘menu_position’ => null,
‘supports’ => array(‘title’,’editor’,’author’,’thumbnail’,’excerpt’,’comments’,’custom-fields’,’revisions’),
‘taxonomies’ => array(‘post_tag’,’category’)
);
register_post_type(‘work’,$args);
}
[/sourcecode]

One final hint is that you will need to flush the cache the first time if this is a change. Add this after the above code and then delete it after you load the page.
[sourcecode language=”php”]add_action(‘admin_init’, ‘flush_rewrite_rules’);[/sourcecode]