- Web templates
- E-commerce Templates
- CMS & Blog Templates
- Facebook Templates
- Website Builders
WordPress. How to create a custom post type
January 26, 2015
This tutorial shows how to create a custom post type in WordPress.
WordPress. How to create a custom post type
Custom post types are content types like posts and pages. Since WordPress evolved from a simple blogging platform into a robust CMS, the term post stuck to it. However, a post type can be any kind of content.
By default, WordPress comes with these post types:
-
Post
-
Page
You can create your own custom post types and call them whatever you want. For example if you run a movie review website, then you would probably want to create a movie reviews post type. This post type can have different custom fields and even its own custom category structure. Other examples of post types are: Portfolio, Testimonials, Products, etc.
In order to create custom post type in WordPress, you should do the following:
-
Open your WordPress dashboard.
-
Go to Appearance -> Editor. In the top right corner select themeXXXXX files to edit.
-
Click Select button to show themeXXXXX files.
-
Select theme-init.php file from the list shown on the screenshot below:
-
In the file you will find all custom post types defined in the template.
-
Each custom post type is highlighted with the php comment.
-
Find the location you want to add new custom post type and paste the following code:
/* Recipes*/ function my_post_type_recipe() { register_post_type( 'recipe', array( 'label' => __('Recipe', CURRENT_THEME), 'public' => true, 'show_ui' => true, 'show_in_nav_menus' => false, 'menu_position' => 5, 'menu_icon' => 'dashicons-book-alt', 'rewrite' => array( 'slug' => 'recipes-view', 'with_front' => FALSE, ), 'supports' => array( 'title', 'thumbnail', 'editor' ) ) ); } add_action('init', 'my_post_type_recipe');
-
In our case we will add the code above the Services custom post type.
-
Now we can see our new custom post type Recipe added:
-
In order to save changes, please click Update file button.
-
Recipe custom post type is now in your dashboard.
Now let’s change the icon for this custom post type in WordPress dashboard:
-
Find the following line of the code added:
'menu_icon' => 'dashicons-visibility',
-
dashicons-visibility is an icon code – you can replace it with another icon code;
-
Copy the icon code:
-
Replace the icon code in the file:
-
Click Update file button to save the changes. The icon has been changed.
Feel free to check the detailed video tutorial below: