How to Create Custom Post Types in WordPress


WordPress is the most popular and easy to use content management system (CMS) platform right now. And day by day it’s going to more popular because anyone can easily develop any kind of website. A website has different types of content and WordPress use Post Types to divided theme. WordPress comes with these default post types-

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Navigation Menus
  • Custom CSS
  • Changesets

Create Custom Post Types

Now, we’ll create a custom post types by adding code to functions.php file. Go to your child theme functions.php and edit.

Step 1: Add detail option to Custom Post Type

* Create our Custom Post Type
function custom_post_type_books() {
	// Custom Post Type UI labels 
    $labels = array(
        'name'				  => _x( 'Books', 'Post Type General Name'),
        'singular_name'       => _x( 'Book', 'Post Type Singular Name'),
        'menu_name'           => __( 'Books'),
        'parent_item_colon'   => __( 'Parent Book'),
        'all_items'           => __( 'All Books'),
        'view_item'           => __( 'View Book'),
        'add_new_item'        => __( 'Add New Book'),
        'add_new'             => __( 'Add New'),
        'edit_item'           => __( 'Edit Book'),
        'update_item'         => __( 'Update Book'),
        'search_items'        => __( 'Search Book'),
        'not_found'           => __( 'Not Found'),
        'not_found_in_trash'  => __( 'Not found in Trash'),
	// Set other options for Custom Post Type
    $args = array(
        'label'               => __( 'Books'),
        'description'         => __( 'Book news and reviews'),
        'labels'              => $labels,
        // Which features are supports in Editor
        'supports'            => array( 
								'custom-fields', ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        '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,
        'capability_type'     => 'post',
    // Registering your Custom Post Type
    register_post_type( 'books', $args );
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
add_action( 'init', 'custom_post_type_books', 0 );

Step 2: Display Custom Post Type

We created our custom post type now, need to display it on our website. We’ll display our custom post type books in default post and archive template. What we’ve to do is just duplicate the single.php file and rename it single-books.php. Now go to Settings->Permalinks->Post Name and click ‘Save Changes’. Now single posts and archives both will work fine. If the archive page doesn’t work then duplicate the archive.php and rename it archive-books.php

Leave a Reply

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like