How To Create A Custom Post Type

When Custom Post Types was introduced in WordPress 3.0, it took WordPress to the whole new level. Instead of just a blogging platform, Custom Post Type gives more flexibility and extends WordPress to be a CMS. It helps to distinguish different types of content on your WordPress site - Post, Page, Attachment, Revision and Navigation Menu - thus make it easier to manage your content.

For example, if you are a Web Design agency, you probably want to showcase your portfolio on your site. By creating a Portfolio custom post type, it’ll make it easier for you to manage your portfolio by differentiating it from other posts or pages. The CPT can be added to your WordPress site via register_post_type function.

In this example, we’ll show you how to create a simple Portfolio custom post type. Of course, you can change the name to anything you want.

portfolio custom post type

SImply put the following code in your functions.php file or even better your functionality plugin:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
// Create portfolio custom post type
add_action( 'init', 'register_cpt_portfolio' );
function register_cpt_portfolio() {
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolio', 'portfolio' ),
'singular_name' => __( 'Portfolio', 'portfolio' ),
),
'exclude_from_search' => true,
'has_archive' => true,
'hierarchical' => true,
'public' => true,
'rewrite' => array( 'slug' => 'portfolio' ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
)
);
}
view raw functions.php hosted with ❤ by GitHub

There are lots of parameters can be accepted by the function to further customize your Custom Post Type. Please see the WordPress Codex for more info.

Here’s another example with lot more parameters:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<?php
// Create a Portfolio Post Type
add_action( 'init', 'register_cpt_portfolio' );
function register_cpt_portfolio() {
$labels = array(
'name' => _x( 'Portfolio', 'portfolio' ),
'singular_name' => _x( 'Portfolio', 'portfolio' ),
'add_new' => _x( 'Add New', 'portfolio' ),
'add_new_item' => _x( 'Add New Portfolio', 'portfolio' ),
'edit_item' => _x( 'Edit Portfolio', 'portfolio' ),
'new_item' => _x( 'New Portfolio', 'portfolio' ),
'view_item' => _x( 'View Portfolio', 'portfolio' ),
'search_items' => _x( 'Search Portfolio', 'portfolio' ),
'not_found' => _x( 'No portfolio found', 'portfolio' ),
'not_found_in_trash' => _x( 'No portfolio found in Trash', 'portfolio' ),
'parent_item_colon' => _x( 'Parent Portfolio:', 'portfolio' ),
'menu_name' => _x( 'Portfolio', 'portfolio' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Some of our works.',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail' ),
'taxonomies' => array( 'Service' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'portfolio', $args );
}
view raw functions.php hosted with ❤ by GitHub

Of course, you do not need to remember each and every single code. There are few tools available to generate the code for you. For example, take a look at the CPT Generator Tool we introduced before or the one by Themergency.

We just showed you how to create a simple CPT. Of course, there are lots more you can do such as adding custom taxonomies, metabox and more.

Leave a Reply

Your email address will not be published. Required fields are marked *