Create a Custom Post Type in WordPress

In this article, I will show you how you can add custom post type in WordPress manually or using a plugin as well. Most of the bloggers and developers having trouble in managing different post types like news, posts, movies, technologies, whitepapers etc. By default WordPress comes with only two types of posts and pages, it is difficult to manage other post types.

For managing two or more posts type WordPress provides additional functions that can be used to add post type as per the requirement. In this post, I will show you how you can easily add a custom post type.

What is the Custom Post Type?

Custom Post Type is the specific type of post types that can be added to your WordPress using a simple function called the register_post_type(). This function allows adding post type like news, technology, whitepapers etc.

/*
* Creating a function to create our CPT
*/

add_action( 'init', 'my_custom_post_technology' );

function my_custom_post_technology() {
  $labels = array(
    'name'               => _x( 'technologies', 'post type general name' ),
    'singular_name'      => _x( 'technology', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'tech' ),
    'add_new_item'       => __( 'Add New technology' ),
    'edit_item'          => __( 'Edit technology' ),
    'new_item'           => __( 'New technology' ),
    'all_items'          => __( 'All technologies' ),
    'view_item'          => __( 'View technology' ),
    'search_items'       => __( 'Search technologies' ),
    'not_found'          => __( 'No technology found' ),
    'not_found_in_trash' => __( 'No technology found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Technology'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our Technology specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'technology', $args ); 
}

Just you need to copy the above function and paste it to your themes function.php file.

custom post type

So now our post type named “Technology” created. If you want post type with taxonomies like Technology Categories. So just copy below code and paste it to function.php file. And make sure you can replace the name technology to your specific post type name.

add_action( 'init', 'my_taxonomies_technology', 0 );

function my_taxonomies_technology() {
  $labels = array(
    'name'              => _x( 'Technology Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Technology Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Technology Categories' ),
    'all_items'         => __( 'All Technology Categories' ),
    'parent_item'       => __( 'Parent Technology Category' ),
    'parent_item_colon' => __( 'Parent Technology Category:' ),
    'edit_item'         => __( 'Edit Technology Category' ), 
    'update_item'       => __( 'Update Technology Category' ),
    'add_new_item'      => __( 'Add New Technology Category' ),
    'new_item_name'     => __( 'New Technology Category' ),
    'menu_name'         => __( 'Technology Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'technology_category', 'technology', $args );
}

Displaying Post Type Using the new Template

By default, WordPress uses archive template to display all post types. For creating a custom archive template for our custom post type we need to create a new file in our themes folder and name it archive-technology.php. Replace technology with the name of your post type.

For getting started, you can copy the contents of your theme’s archive.php file into the archive-technology.php template. And then start modifying it to meet your needs. Now whenever the archive page for your post type is accessed, this template will be used to display it.

You may also refer WordPress Documentation https://developer.wordpress.org/themes/basics/post-types/

If you like this post, so please leave a comment with your thoughts and share this on your Facebook group(s). Thank you for sharing and being nice!

Leave a Reply