Step By Step Create Custom Post Types Without Plugin

WordPress provides default built-in content types like Posts, Pages, and Media. The well known popular CMS is WordPress providing various customization, which gives a lot of space to build a blog or website as we like. So if you like to create Custom Post Types as same as the Posts? With the few simple functions, you can get the Posts Type as you like. Now I show you step by step create custom post types without plugin.
Create a Custom Post Type in your WordPress Without Plugin
With WordPress, you can create multiple Custom Post Types as you want (Eg. Movies, Resources, Books, etc..). Creating Custom Post Type (CPT) as same as Posts, here we going to create for “Resources” with Resource Posts, Categories, and Tags. The following function will help you to create Resources Custom Post Type, for this open your theme folder and add the below code in Appearance-> Theme Editor-> functions.php.
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 47 |
//===== Resources Custom Type ======// function custom_resource_type() { // Labels for Custom Post Type $labels = array( 'name' => _x( 'Resources', 'Post Type General Name', 'gucherry-blog' ), 'singular_name' => _x( 'Resource', 'Post Type Singular Name', 'gucherry-blog' ), 'menu_name' => __( 'Resources', 'gucherry-blog' ), 'parent_item_colon' => __( 'Parent Resource', 'gucherry-blog' ), 'all_items' => __( 'All Resources', 'gucherry-blog' ), 'view_item' => __( 'View Resource', 'gucherry-blog' ), 'add_new_item' => __( 'Add New Resource', 'gucherry-blog' ), 'add_new' => __( 'Add Resource', 'gucherry-blog' ), 'edit_item' => __( 'Edit Resource', 'gucherry-blog' ), 'update_item' => __( 'Update Resource', 'gucherry-blog' ), 'search_items' => __( 'Search Resource', 'gucherry-blog' ), 'not_found' => __( 'Not Found', 'gucherry-blog' ), 'not_found_in_trash' => __( 'Not found in Trash', 'gucherry-blog' ), ); // Customize option for Custom Post Type $args = array( 'label' => __( 'resources', 'gucherry-blog' ), 'description' => __( 'Resource for software products and online', 'gucherry-blog' ), 'labels' => $labels, // Custom Post Type supports features in the Post Editor 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ), // You can associate this Custom Post Type with a taxonomy or custom taxonomy. 'taxonomies' => array( '' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'menu_icon' => 'dashicons-admin-site-alt2', 'capability_type' => 'page', ); // Registering your Custom Post Type register_post_type( 'resources', $args ); } add_action( 'init', 'custom_resource_type', 0 ); |
Note: You need to replace the gucherry-blog to your theme folder name.
Once you update the above code in your functions.php, you get the Resources Posts in the WordPress Admin Panel and it looks like the below screenshot.
Create a Category for your Custom Post Type
If you like to create a category only for created Custom Post Type, here you have a simple solution to add new taxonomy, execute it hierarchical like categories. Use the below code in your functions.php and you get Custom Post Type category.
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 |
//====== Categories for Resources Custom Type ========// function resource_categories_taxonomy() { // Add new taxonomy, make it hierarchical like categories //first do the translations part for GUI $resource_cats = array( 'name' => _x( 'Resource Categories', 'taxonomy general name', 'gucherry-blog' ), 'singular_name' => _x( 'Resource Category', 'taxonomy singular name', 'gucherry-blog' ), 'search_items' => __( 'Search Resource Categories', 'gucherry-blog' ), 'all_items' => __( 'All Resource', 'gucherry-blog' ), 'parent_item' => __( 'Parent Resource', 'gucherry-blog' ), 'parent_item_colon' => __( 'Parent Resource:', 'gucherry-blog' ), 'edit_item' => __( 'Edit Resource', 'gucherry-blog' ), 'update_item' => __( 'Update Resource', 'gucherry-blog' ), 'add_new_item' => __( 'Add New Resource', 'gucherry-blog' ), 'new_item_name' => __( 'New Resource', 'gucherry-blog' ), 'menu_name' => __( 'Resource Categories', 'gucherry-blog' ), ); // Now register the taxonomy. Replace the parameter resources withing the array by the name of your custom post type. register_taxonomy('resource_categories',array('resources'), array( 'hierarchical' => true, 'labels' => $resource_cats, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'resource' ), )); } add_action( 'init', 'resource_categories_taxonomy', 0 ); |
Once you updated the function on your functions.php, then output for Custom Post Type Category looks like the below screenshot.
Create a Tags for your Custom Post Type
As same as the above category creation here we going to create for tags. Use the below function to get the tags option on your created Custom Post Type.
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 |
//======== Tags for Resources Custom Type ==========// function resource_tags_taxonomy() { // Labels part for the GUI $resource_tags = array( 'name' => _x( 'Resource Tags', 'taxonomy general name', 'gucherry-blog' ), 'singular_name' => _x( 'Resource Tag', 'taxonomy singular name', 'gucherry-blog' ), 'search_items' => __( 'Search Resource Tags', 'gucherry-blog' ), 'popular_items' => __( 'Popular Resource Tags', 'gucherry-blog' ), 'all_items' => __( 'All Resource Tags', 'gucherry-blog' ), 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => __( 'Edit Resource Tag', 'gucherry-blog' ), 'update_item' => __( 'Update Resource Tag', 'gucherry-blog' ), 'add_new_item' => __( 'Add New Resource Tag', 'gucherry-blog' ), 'new_item_name' => __( 'New Resource Tag Name', 'gucherry-blog' ), 'separate_items_with_commas' => __( 'Separate resources tags with commas', 'gucherry-blog' ), 'add_or_remove_items' => __( 'Add or remove resource tags', 'gucherry-blog' ), 'choose_from_most_used' => __( 'Choose from the most used resource tags', 'gucherry-blog' ), 'menu_name' => __( 'Resource Tags', 'gucherry-blog' ), ); // Now register the non-hierarchical taxonomy like tag. Replace the parameter Resources withing the array by the name of your custom post type. register_taxonomy('resource_tags','resources',array( 'hierarchical' => false, 'labels' => $resource_tags, 'show_ui' => true, 'show_admin_column' => true, 'update_count_callback' => '_update_post_term_count', 'query_var' => true, 'rewrite' => array( 'slug' => 'resource tags' ), )); } add_action( 'init', 'resource_tags_taxonomy', 0 ); |
Output for the created Custom Post Type tags.
The final output for Custom Post Type:
Once you completed all these steps now you need to display the created Custom Post Type posts in Frontend. For that here you have an article.
Conclusion:
I hope this article will help you to create the Custom Post Type for your WordPress website. If you have any doubts about the topic let me know in the comment box. If you like this article share it with your friends.
CPT CategoryCPT TagsCustom Post TypesWithout Plugin
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]