A few days ago, I told you about creating Monthly Custom Post Type Archives, today I want to show you how to utilize the new native archive capability built into WordPress 3.1.
In order for this tutorial to make any sense, you need to know how to create custom post types. If you do not, then I highly suggest reading Justin Tadlock’s great article about the topic.
Let’s presume that we have created a custom post type like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | add_action( 'init', 'create_books_post_type' ); function create_books_post_type() { register_post_type( 'books', array( 'labels' => array( 'name' => __('Books'), 'singular_name' => __('Book') ), 'public' => true, 'show_ui' => true, 'rewrite' => array( 'slug' => 'book', 'with_front' => false ) ) ); } |
WordPress 3.1 has introduced a new argument we can pass to our custom post type init function called has_archive. By including this argument, WordPress will create an archive page for the post type. So now our custom post type init looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | add_action( 'init', 'create_books_post_type' ); function create_books_post_type() { register_post_type( 'books', array( 'labels' => array( 'name' => __('Books'), 'singular_name' => __('Book') ), 'public' => true, 'show_ui' => true, 'rewrite' => array( 'slug' => 'book', 'with_front' => false ), 'has_archive' => true ) ); } |
Now we can access our Books archive by going to site.com/book. The archive name is determined by the slug argument by default, but if we wish, we can change it by doing:
1 | 'has_archive' => 'book-list' |
which would render as site.com/book-list.
By setting has_archive to true, or any custom value, you can also create your own template for the archive display. By default the page will display with archive.php, but you can use your own custom template by creating a file with the name archive-{$post_type}.php. In this case, our post type name is books, so our archive file would be archive-books.php.






This works for me. A question:how can I add a "taxonomy filter" ? ie: site.com/book-list/CATEGORY_BASE/rowlingThanks
- spam
- offensive
- disagree
- off topic
Like