Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 7 • Making the Most of WordPress Plugins 187


'query_var' => true,
'rewrite' => true
)
);


}
?>


CREATING A CUSTOM POST TYPE


Creating a custom post type is simple enough. The following code can be used in functions.
php, but you are better off putting it in a plugin. The key to everything is
register_post_type():


// Register post type
register_post_type( 'smashing_post_type',
array(
'labels' => array(
'name' => 'Smashing Post Type'
),
'singular_label' => 'Smashing Post Type',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'show_in_menu' => true,
'supports' => array( 'title', 'editor', 'author', 'revisions', 'comments' )
)
);


This will register a new post type, called smashing_post_type, which you’ll find just
before the array with the settings for the post type. These settings alter how things look and
feel — for example, what’s on the labels in the admin menu, whether this is something that is
public, if there’s supposed to be a menu option at all, and what sort of user level is needed to
be able to use it. Most of it is pretty self-explanatory, but the supports array within array
is interesting. This is where you decide what the post type supports: In this case, you’ve got a
title, the Write Post Editor box, the author, revisions, and comments. No taxonomies at all, no
excerpt, no custom fields — everything is set here.


Just pasting this code into any theme’s functions.php will make the custom post type appear
in the admin interface, as shown on Figure 7-4. You’ll most likely have to rebuild your
permalinks when this is done; otherwise, the links to the post and archives might not work.

Free download pdf