Creating Custom Post types in WordPress
May 11, 2015
We all know that WordPress is a completely flexible platform for developing websites and blogs. It offers endless possibilities to people to create the websites as per their needs. Here, we are going to discuss about an important feature of WordPress i.e. to create custom Post types.
What are Custom Post Types?
Suppose, you have a mobile site and you are in search of a method that automatically helps you to post the products you wish to sale on the website. Such type of content needs frequent updates and this can be easily done with Custom Post types.
Let’s say you wish to sell some electronic items on your site hence in order to get the products posted automatically, you can create a custom post type called “Electronic items” and this will allow you to add certain specific content related to electronic items thus keeping it separate from other products.
Further, you can also create individual custom post types in Electronics; say a separate post type for mobile phones, separate for laptops and more and then you can offer certain discounts, special offers on the related post type.
How Custom Post types can help you?
Custom post types are useful when a website or blog needs separate sections for different products or categories. Some websites also need a separate content structure where admin can post particular details like as event dates, images and other such relevant information.
In order to make user friendly admin panel, developers can create custom post type with the name “Manage Events” and then the admin can get in there and add the respective content as well as related information accordingly.
This makes it easy, clear and simple to understand even for a non tech savvy person who is involved as an admin. If we talk about the technical aspects of Custom Post types then here are some sample codes and process which will show you how these can be easily implemented in your WordPress Set up:
Steps to implement Custom Post types in WordPress:
- First register the information with your content management system and for this some particular information must be included in the system which makes it easy for WordPress to understand the Custom Post type:
Code to be used:
function my_post_type(){
$args = array(
'label' => 'My Custom Post Type',
'show_ui' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'post-slug'),
'menu_icon' => 'dashicons-video-alt',
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes')
);
register_post_type('custom-post', $args);
}
add_action('init', 'my_post_type');
This code is to be included in function.php file as this will make it easier for WordPress to access these files and some other codes that are to be used are as follows:
- function my_post_type(){: This is to be used to begin the process of creating custom post type.
- Label: This part of the code is used to name the post type let’s say “electronic items”. By default, it will be “Custom post type”.
- Show_ui: This is used to develop an user interface of custom post types. This determines the URL of custom post types which looks similar to other elements of WordPress admin.
- Hierarchial: This is used to determine the hierarchy of custom post types like as child post, parent post or other sub posts.
- Rewrite: This determines the URL of the CPT once the root of custom post type browser.
These are some of the main parts of the code which are used to make custom post type ready for backend and then everything is set to begin with and using the code anywhere in the WordPress Setup.
Wind Up
Well, this is not the end. WordPress offers innumerable options to develop and customize things in your blog or website. To know more about WordPress features and its usability, you can get in touch with Softqube Technologies, PHP Development Company in India.
Share on