How to create new post type on WordPress
‘WordPress’s Different available post types are not Enough ? , lets create your own custom Post types on WordPress on few quick easy steps.’
WordPress houses several different content forms and is categorized into anything called Post Styles. A single object is called a post but this is also the name of a common form of post called posts. By default WordPress comes with a few different types of post that are all placed under the table of wp posts in the database.
We only get three built-in content forms on the backend when we install WordPress, i.e. blogs, sites, and media. WordPress has been fairly versatile and advanced today though. And the approach to introducing more forms of posts has also diversified. The diversified use requires more types of content, because posts, pages and media are not enough and this is where WordPress custom post type comes in handy.
Refer WordPress post and default post types available here
What is a WordPress Custom Post Type?
According to WordPress Codex, “Custom Post Types” also known as “Custom Content Types” is the specific type of post types that can be added to your WordPress using a simple function called the register_post_type()
. The function allows you to add the new custom post type in accordance with a number of specifics such as supported features, availability, and labels.
Create a WordPress Custom Post Type
To create a custom post type for any particular theme on WordPress, navigate to function.php file from your WordPress theme directory then add the following code to it.
/* Custom Post Type Start */
function create_posttype() {
register_post_type( 'news',
// CPT Options
array(
'labels' => array(
'name' => __( 'news' ),
'singular_name' => __( 'News' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'news'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
/* Custom Post Type End */
After adding this code, the News post-type will automatically appear in the Admin Area of your WordPress. To see how it will appear at the front-end of your WordPress dashboard, refer to the image below.
When you create custom post types, it is necessary to use init for the hook in add_action(). The register_post_type()
the function takes the arguments.
/*Custom Post type start*/
function cw_post_type_news() {
$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
$labels = array(
'name' => _x('news', 'plural'),
'singular_name' => _x('news', 'singular'),
'menu_name' => _x('news', 'admin menu'),
'name_admin_bar' => _x('news', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New news'),
'new_item' => __('New news'),
'edit_item' => __('Edit news'),
'view_item' => __('View news'),
'all_items' => __('All news'),
'search_items' => __('Search news'),
'not_found' => __('No news found.'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('news', $args);
}
add_action('init', 'cw_post_type_news');
/*Custom Post type end*/
Create a New Post
With the above code you have successfully created a new post type “news” in the WordPress, Now you can create new posts under that new post type.
You can also create a new post type template in WordPress.
Know more about using https in your website.