Custom post types with custom taxonomies
April 19, 2010 by admin · Leave a Comment
Creating a custom post type associated with custom taxonomies. For those who don’t know, a taxonomy is a term (such as category, tag or anything else) related to posts. For more information about taxonomies, you should have a look at WordPress Codex.
In this example, we’ll create a custom post type named Albums, which belong to “Genres” (the custom categories) and have “Performer” as tags. This snippet has to be pasted in your functions.php file. With those 27 lines of codes, you can create a fully functional music albums archive. Ain’t that powerful?
function post_type_albums() {
register_post_type(
'albums',
array('label' => __('Albums'),
'public' => true,
'show_ui' => true,
'supports' => array(
'post-thumbnails',
'excerpts',
'trackbacks',
'comments')
)
);
// Adding the Custom Taxonomy for Genres. Here we can create categories specific for this post type.
register_taxonomy( 'genres', 'albums', array( 'hierarchical' => true, 'label' => __('Genres') ) );
// Adding the Custom Taxonomy for Performer. Here we can add tags specific for this post type.
register_taxonomy( 'performer', 'albums',
array(
'hierarchical' => false,
'label' => __('Performer'),
'query_var' => 'performer',
'rewrite' => array('slug' => 'performer' )
)
);
}
add_action('init', 'post_type_albums');
Related posts: