Nonhierarchical Postlike Content Types

Let's start transforming the courses by creating a new content type for a university. Listing 12-12 shows a simple plugin that creates a new nonhierarchical content type—something very similar to posts. As with register_taxonomy(), you don't have to include arguments if you plan to use the default values.

Listing 12-12. Creating a non-hierarchical content type for courses function post_type_courses() { register_post_type( 'course', array(

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

register_taxonomy_for_object_type('category', 'course');

In this case, you've decided that the course type should not support comments or trackbacks and will use categories but not tags.

If you activate this little plugin, your navigation menu will immediately gain a new section just below Comments: Courses, with options to edit, add a new course, or manage course categories (Figure 12-12). Tags aren't shown because you didn't add post_tag to the list of fields this content type supports. Also, note that "Series" appears under Posts, but not Courses. (The Series plugin was still activated from another project, and the plugin's taxonomy is assigned only to posts.)

Figure 12-12. Activating the course plugin; new Courses menu section now available

Now you need to change your old course posts to the new course content type. All content types are stored in the wp_posts table, and the only difference at the database level is the post_type field. If you don't want to copy and paste all your old posts into the new content type, you could just edit the database records directly (Figure 12-13) or write a SQL query to do this for all the posts you need to convert.

Figure 12-13. Changing the content type in the wp_posts table

Now, go to Courses ^ Edit and see all the posts you converted to courses (in this case, just one, as shown in Figure 12-14). If you publish this course, you can see it at its new permalink (Figure 12-15). Note that permalinks for custom content types are not editable; they follow the rewrite rules specified in the register_post_type() arguments.

Figure 12-14. Editing the new course

Word Press

ENGL 412: Shakespeare nstua ev otvi tr ^tocn ii.M'i i isn

Instructor: W. H. Auden Email: jiudcagcuiiuilc.cdu

Course Description: An overview of three tragedies (Othello, Monster, and King Lear) and two hiitory plays (Anthony ami Cleopatra and Henry V}.

Figure 12-15. The new course as it appears on the site

Now you have a post-like content type for your courses. However, the office might need to create several related entries for each course: a general overview, a syllabus, and a reading list. In that case, it would make more sense to create the courses as hierarchical content types, like pages.

0 0

Post a comment