How to Create Custom Post Types in WordPress

by emuslearner
0 comment

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: Register a post type

First, we’ll register a post type called Books with an array of arguments. These arguments contain labels, public visibility, archive, and slug.

// Custom post type function
function create_post_type() {
 
    register_post_type( 'books',
    // Array of arguments which are our CTP options
        array(
            'labels' => array(
                'name' => __( 'Books' ),
                'singular_name' => __( 'Book' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'books'),
        )
    );
}
// Adding our function to theme setup
add_action( 'init', 'create_post_type' );

Step 2: 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( 
								'title',
								'editor', 
								'excerpt', 
								'author', 
								'thumbnail', 
								'comments', 
								'revisions', 
								'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 3: 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

0 comment

More Posts in this topic

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: