WordPress Checklist – Custom Post Types

Custom Post Types are really easy to create through the functions.php file rather than installing a 3rd party plugin. Why use a plugin when your can create one with 10 lines of code?

Before I create a custom Post Type I ask myself “Do I need one?”

Do I need one?

With Advanced Custom Fields you can create repeater fields within a page or post which is easily added to your template page.php or single.php file.

For example if I was creating a Gallery I shouldn’t create a Post Type called “Gallery” and add each image as an individual post. I should create a custom repeater field.

Good examples when you need CPT

layout-001

When building wordpress sites I always make everything editable, even in the header and footer.

To achieve this I create a CPT called “layout” and make it appear under the “Appearance” tab in the CMS (see picture).

When in this post type I would create Posts that reflect the area I am making editable for example:

  • Header
  • Sidebar
  • Footer

Using Advanced Custom Fields I can create unique fields for each individual posts as show below.

header-screen

An other good example for a CPT would be for a Careers or Jobs section where you need multiple posts/job openings.

See below the code for my two examples. You would place this in your active themes functions.php file.

################################################
#
#  Create Custom Post Types
#
################################################

function theme_layout() {

        register_post_type( 'layout', array(

                'labels' => array(

                        'name' => 'Layout',

                        'singular_name' => 'layout',

                ),

                'public' => false,

                'exclude_from_search' => true,

                'show_ui' => true,

                'show_in_menu' => 'themes.php',

                'supports' => array( 'title' ,'thumbnail', 'editor' ),

        ) );

}

add_action( 'init', 'theme_layout' );

function careers() {

         register_post_type( 'jobs', array(

                 'labels' => array(

                         'name' => 'Careers',

                         'singular_name' => 'career',

                 ),

                 'public' => true,

                 'exclude_from_search' => false,

                 'show_ui' => true,

                 'rewrite' => array('slug' => 'career','with_front' => false),

                 'supports' => array( 'title' ,'thumbnail', 'editor' ),

         ) );

}

add_action( 'init', 'careers' );