How to Create Custom Post Types in WordPress

Open your functions.php file of your active child theme (or theme) and add the code below to it. Make sure the code is added before the closing PHP tag. I will explain the code below.

add_action( 'init', 'ls_register_post_types' ); 
function ls_register_post_types() { 
register_post_type( 'cst_new', array( 'labels' => array(
						'name' => 'News',
						'singular_name' => 'news'
							),
					'public'      => true,
					'has_archive' => true,
					'rewrite'     => array('slug' => 'news','with_front' => false),
					'supports'    => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'post-formats'),
					'can_export'  => true,
					)
			);
register_taxonomy('news_cat', 'cst_new', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' =>array( 'slug' => 'cat-news' )));
register_taxonomy('news_tag', 'cst_new', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));
}

What this code does is that it registers a post type ‘News’ with an array of arguments. These arguments are the options of our custom post type. This array has two parts, the first part is labels, which itself is an array. The second part contains other arguments like public visibility, has archive, and slug that will be used in URLs for this post type.

After adding the code you need to head over to Dashboard. You will notice the new Custom post type of News are there with categories and Tags.

Leave a Comment

Scroll to Top