Understanding and Creating a WordPress custom post type

Posted By Harsh Soni | 30-Sep-2018

 

Wordpress is always taken a blog management system, but nowadays it's becoming popular for the web development. It provides us with a handful of content types by default like posts, pages, attachments, revisions, media, and navigation menus. Some of them work in the background and serve their purpose quietly and don't need much attention

 

Posts and pages are the most common thing from which user interacts. They are much similar and the only difference between them is that posts can be accessed from your wp-admin thought the post menu, while the pages can be accessed through the pages.

 

 

What is Custom Post Type?

 

A post type which is defined by you.  It is a content type – defined by you – which will appear in the wp-admin menus in the sidebar. And it will be similar to the one you were working with.

 

Creating a Custom Post Type?

 

Creating a custom Post type is easy and it can be done in two ways 

 

  1. Installing Custom Post type plugin
  2. Using custom code in the functions.php file 

 

Here I would prefer to go with the second option that is to add custom code to the functions.php file. As it provides more control over the functionality on the code level. So here is a sample code to add a custom post type "car_product".

 

function create_post_type() {
  register_post_type( 'car_product',
    array(
      'labels' => array(
        'name' => __( 'Cars' ),
        'singular_name' => __( 'Car' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

 

It creates a post type named Car which is identified as car_product. The register_post_type() function receives two major arguments. The first one is labels and the second one is public which is a predefined flag to show the post type on the administration screens. You can understand more about creating Wordpress Custom Post type here.

 

Request for Proposal

Recaptcha is required.

Sending message..