Custom Taxonomies for Custom Content Types

Let's say the department in question is the Honors office. This office tracks the Honors courses offered in each college. Before custom content types came along, they would use categories for each college, but those categories were mixed in with the general blog categories (news, scholarships) and the office's podcast.

You can instead use a custom taxonomy for your colleges. Once you've created the taxonomies, you have to register them for the course content type. Listing 12-14 shows the complete revised plugin. You've removed the standard category taxonomy and replaced it with a new one, colleges, which is defined below.

The resulting changes to the Courses ^ Edit screen are shown in Figure 12-17. For the rest of this chapter, let's stick with a non-hierarchical content type.

Listing 12-14. Adding custom taxonomies to the course content type

Plugin Name: Course Post Types */

add_action('init', 'post_type_courses'); function post_type_courses() {

register_post_type( 'course', array(

'supports' => array( 'title', 'editor', 'author', 'excerpt', 'custom-fields', 'revisions',)

register_taxonomy_for_object_type('college', 'course');

add_action('init', 'create_course_series_tax');

register_activation_hook( _FILE_, 'activate_course_series_tax' );

function activate_course_series_tax() { create_course_series_tax(); $GLOBALS['wp_rewrite']->flush_rules();

function create_course_series_tax() { register_taxonomy( 'college', 'course', array(

Figure 12-17. Editing a course with the new taxonomy

Was this article helpful?

0 0

Post a comment