How to Change the Name of a WordPress Category
Installing and Configuring the WordPress.com Stats Plugin
Edit a WordPress Theme to Include Photo Gallery Styles

How to Create a Custom Post Type in Your WordPress Theme

To create and use custom post types on your site, you need to be sure that your WordPress theme contains the correct code and functions. In the following steps, you create a basic and generic custom post type called “Generic Content”:

  1. On your Dashboard, choose Appearance→Editor.

    The Edit Themes page opens.

  2. Click the Theme Functions template link to open the functions.php file.

    The Theme Functions template opens in the text editor in the middle of the page.

  3. Add the custom post type code to the bottom of the Theme Functions template.

    Scroll down to the bottom of the functions.php file and include the following code to add a Generic Content custom post type to your site:

    // ADD CUSTOM POST TYPE
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
     register_post_type( 'generic-content',
     array(
     'labels' => array(
     'name' => __( 'Generic Content' ),
     'singular_name' => __( 'Generic Content' )
     ),
     'public' => true
     )
     );
    }

    The function register_post_type(); can accept several arguments and parameters. You can use a variety and a combination of arguments and parameters to create a specific post type. You can find more information on custom post types and using the register_post_type(); function on the WordPress Codex page.

  4. Click the Update File button to save the changes made to the functions.php file.

If you don’t feel up to writing all this code in the Theme Functions file, the Custom Post Type UI plugin from webdevStudios provides you with an easy interface on your WordPress Dashboard. This plugin also simplifies the creation of custom post types on your site and bypasses the need to create the code in the theme functions (functions.php) file.

After you complete the steps to add the Generic Content post type to your site, it’s added to the left navigation menu on the Dashboard.

image0.jpg

You add and publish new content using the new custom post type just as you would when you write and publish blog posts. The published content isn’t added to the chronological listing of blog posts, but is instead treated as separate content, just like static pages.

View the permalink for the custom post type, and you see that it adopts the post type name, Generic Content, and uses it as part of the permalink structure. The permalink looks like http://yourdomain.com/generic-content/new-article.

Here is a real-life example used on the No Rules Theatre Company site. This site uses a Shows custom post type to create custom content for the shows that the theatre produces each season.

// ADD CUSTOM POST TYPE: SHOWS
add_action( 'init', 'create_my_post_types' );
function create_my_post_types() {
    register_post_type( 'shows',
    array(
    'labels' => array(
    'name' => __( 'Shows' ),
    'singular_name' => __( 'Show' ),
    'add_new' => __( 'Add New Show' ),
    'add_new_item' => __( 'Add New Show' ),
    'edit' => __( 'Edit' ),
    'edit_item' => __( 'Edit Show' ),
    'new_item' => __( 'New Show' ),
    'view' => __( 'View Show' ),
    'view_item' => __( 'View Show' ),
    'search_items' => __( 'Search Shows' ),
    'not_found' => __( 'No shows found' ),
    'not_found_in_trash' => __( 'No shows found in Trash' ),
    'parent' => __( 'Parent Show' ),
    ),
    'public' => true,
    'show_ui' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'menu_position' => 10,
    'menu_icon' => get_stylesheet_directory_uri() . '/img/nrt-shows.png',
    'hierarchical' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'shows', 'with_front' => false ),
    'taxonomies' => array( 'post_tag', 'category'),
    'can_export' => true,
    'supports' => array(
    'post-thumbnails',
    'excerpts',
    'comments',
    'revisions',
    'title',
    'editor',
    'page-attributes',
    'custom-fields')
    )
    );
}

To add custom post types to the Menus options on the Menus page accessible from the Dashboard (choose Appearance→Menus), click the Screen Options tab at the top right of that page.

You see a check box next to Post Types that you can select to enable your custom post types in the menus you create. The Post Types appear in the Screen Options only if you have custom post types enabled in your theme.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Customize Backgrounds in Twenty Ten WordPress Themes
How to Add Widgets to Your WordPress.com Blog Sidebar
How to Search for WordPress Plugins from the Dashboard
The Settings Pages for 3 Popular WordPress Plugins
How to Upload Header Images in Twenty Thirteen WordPress Themes
Advertisement

Inside Dummies.com