Custom Post Types

One of the most confusing features of WordPress is custom post types. It is also a useful, powerful, and easy feature to implement and use after you understand how it works. WordPress has five default post types:

  • Post: The most commonly used post type. Content appears in a blog in reverse sequential time order.
  • Page: Similar to a post, but pages don't use the time-based structure of posts. Pages can be organized in a hierarchy and have their own URLs off the main site URL.
  • Attachment: This special post type holds information about files uploaded through the WordPress Media upload system.
  • Revisions: This post type holds past revisions of posts and pages as well as drafts.
  • Nav Menus: This post type holds information about each item in a navigation menu.

A post type is really a type of content stored in the wp_posts table in the WordPress database. The post type is stored in the wp_posts table in the post_type column. The information in the post_type column differentiates each type of content so that WordPress, a theme, or a plugin can treat the specific content types differently.

When you understand that a post type is really just a method to distinguish how different content types are used, you can investigate custom post types.

Say you have a Web site about movies. Movies have common attributes — actors, directors, writers, and producers. But say you don't want to store your movie information in a post or a page because it doesn't fit either content type. This is where custom post types become useful. You can create a custom post type for movies and apply the common attributes of actors, directors, and so on. A theme can handle movies differently than a post or a page by having a custom template for the movies post type and create different styling attributes and templates for the movies post type. You can search and archive movies differently than you can with posts and pages.

Here's how to create a simple custom post type in WordPress by adding these lines of code into the Theme Functions template file (located in the theme file and called functions.php):

add_action('init','create_post_type'),
function create_post_type() {
  register_post_type( 'movies',
    array(
      'labels' => array(
        'name' => ('Movies'),
        'singular_name' => ('Movie'),
        'rewrite' => array('slug' => 'movies'),
      ),
      'public' => true,
    )
  );
}

Let's see what's going on in the code:

  • The first line is the action hook. This uses ‘init’ so that it's called on the front end and in the Dashboard to display the custom post type in both.
  • The callback function starts with the register_post_type function and the custom post type name. This is what creates the custom post type and gives it properties.
  • Next is an array of arguments that are the custom post type properties.
  • The ‘labels’ arguments include the name that displays on the Dashboard menu, the name that will be used (Movies), and what is used for the slug in the URL to the posts(http://yourdomain.com/movies, for example) in this custom post type.
  • The ‘public’ argument controls whether the custom post type displays in the Dashboard.

Figure 7-5 shows how the Custom Post Type page and menu item look in the Dashboard.

Figure 7-5: A custom post type in the WordPress Dashboard.

image

Figure 7-6 shows a custom post type on a Web site.

Figure 7-6: A custom post type shown on a Web site.

image

Many other arguments associated with register_post_type give this function its real power. For full documentation on all the arguments and the use of this function, check out http://codex.wordpress.org/Function_Reference/register_post_type. (Custom post types are also discussed in detail in Book VI, Chapter 6.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.53