How to create custom post type along with taxonomy

In WordPress custom post type plays vital role to manage administration panel.

What is custom post type?

Suppose you want your blog to have separate section related to resorts and countries where they are situated. By using custom post type you can create a new section where you can store any kind of information. Custom post has dedicated editor, thumbnail, media editor, input fields uses the existing WordPress table structure for ease in data management.  Apart from this that, custom post types are SEO friendly because of its dynamic permalinks.

Let’s Create a Custom Post Type Plugin

Here we shall create a custom post type plugin which will be able to manage resorts and their countries.

Let us get started.

Step 1: Create WordPress Plugin Directory

Open your WordPress Plugin directory and create a new directory called Resorts

Step 2: Create PHP File

Open the directory and create a PHP file named resorts.php.

Step 3: Add Header

Open the file and add the appropriate header at the top.

<?php

/*Plugin Name: Resorts

Plugin URI: https://manudhaliwal.wordpress.com

Description: Declares a plugin that will manage Resorts and their countries

Version: 1.0

Author: Dharminder Singh */?>

Register your function to create custom post type: 

function my_resorts(){

$labels = array(

‘name’                  => _x( ‘Manage Resorts’, ‘Post Type General Name’, ‘cf’ ),

‘singular_name’         => _x( ‘Manage Resort’, ‘Post Type Singular Name’, ‘cf’ ),

‘menu_name’             => __( ‘Manage Resort’, ‘cf’ ),

‘name_admin_bar’        => __( ‘Resort’, ‘cf’ ),

‘archives’              => __( ‘Resort Archives’, ‘cf’ ),

‘attributes’            => __( ‘Resort Attributes’, ‘cf’ ),

‘parent_item_colon’     => __( ‘Parent Resort:’, ‘cf’ ),

‘all_items’             => __( ‘All Resort’, ‘cf’ ),

‘add_new_item’          => __( ‘Add New Resort’, ‘cf’ ),

‘add_new’               => __( ‘Add New Resort’, ‘cf’ ),

‘new_item’              => __( ‘New Resort’, ‘cf’ ),

‘edit_item’             => __( ‘Edit Resort’, ‘cf’ ),

‘update_item’           => __( ‘Update Resort’, ‘cf’ ),

‘view_item’             => __( ‘View Resort’, ‘cf’ ),

‘view_items’            => __( ‘View Resort’, ‘cf’ ),

‘search_items’          => __( ‘Search Resort’, ‘cf’ ),

‘not_found’             => __( ‘Not resort found’, ‘cf’ ),

‘not_found_in_trash’    => __( ‘Not found in Trash’, ‘cf’ ),

‘featured_image’        => __( ‘Featured Image’, ‘cf’ ),

‘set_featured_image’    => __( ‘Set featured image’, ‘cf’ ),

‘remove_featured_image’ => __( ‘Remove featured image’, ‘cf’),

‘use_featured_image’    => __( ‘Use as featured image’, ‘cf’),

‘insert_into_item’      => __( ‘Insert into item’, ‘cf’ ),

‘uploaded_to_this_item’ => __( ‘Uploaded to this item’, ‘cf’ ),

‘items_list’            => __( ‘Resorts list’, ‘cf’ ),

‘items_list_navigation’ => __( ‘Resorts list navigation’, ‘cf’ ),

‘filter_items_list’     => __( ‘Filter resorts list’, ‘cf’ ),

);

$args = array(

‘label’                 => __( ‘Resort’, ‘cf’ ),

‘description’           => __( ‘Post Type Description’, ‘cf’ ),

‘labels’                => $labels,

‘supports’              => array( ‘title’,’thumbnail’ ),

‘taxonomies’            => array( ‘resort_country,resort_shop’ ),

‘hierarchical’          => false,

‘public’                => true,

‘show_ui’               => true,

‘show_in_menu’          => true,

‘menu_position’         => 5,

‘menu_icon’             => get_template_directory_uri() .’/img/r.png’,

‘show_in_admin_bar’     => true,

‘show_in_nav_menus’     => true,

‘can_export’            => true,

‘has_archive’           => true,

‘exclude_from_search’   => false,

‘publicly_queryable’    => true,

‘capability_type’       => ‘page’,

‘show_in_rest’          => true,

);

register_post_type( ‘resort’, $args );

}

add_action( ‘init’,array($this,’my_resorts’), 0 );

The register_post_type function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including the different sections in the admin. This function takes two arguments: the first one is an unique name of the custom post type and the second one an array demonstrating the properties of the new custom post type.

Activate your plugin and you will see a new post type generated in WordPress administration.

After the successfully creating of custom post type we need to create its taxonomy. which will directly relate to this custom post.To create a post’s taxonomy there are just few lines of code. so it is pretty easy to generate a post type’s category.

function resort_country_taxonomy() {

$labels = array(

‘name’                       => _x( ‘Resort Country’, ‘Taxonomy General Name’, ‘cf’ ),

‘singular_name’              => _x( ‘Resort Country’, ‘Taxonomy Singular Name’, ‘cf’ ),

‘menu_name’                  => __( ‘Resort Country’, ‘cf’ ),

‘all_items’                  => __( ‘All countries’, ‘cf’ ),

‘parent_item’                => __( ‘Parent Item’, ‘cf’ ),

‘parent_item_colon’          => __( ‘Parent Item:’, ‘cf’ ),

‘new_item_name’              => __( ‘New Country Name’, ‘cf’ ),

‘add_new_item’               => __( ‘Add New Country’, ‘cf’ ),

‘edit_item’                  => __( ‘Edit Country’, ‘cf’ ),

‘update_item’                => __( ‘Update Country’, ‘cf’ ),

‘view_item’                  => __( ‘View Country’, ‘cf’ ),

‘separate_items_with_commas’ => __( ‘Separate items with commas’, ‘cf’ ),

‘add_or_remove_items’        => __( ‘Add or remove items’, ‘cf’ ),

‘choose_from_most_used’      => __( ‘Choose from the most used’, ‘cf’ ),

‘popular_items’              => __( ‘Popular Items’, ‘cf’ ),

‘search_items’               => __( ‘Search Items’, ‘cf’ ),

‘not_found’                  => __( ‘Not Found’, ‘cf’ ),

‘no_terms’                   => __( ‘No items’, ‘cf’ ),

‘items_list’                 => __( ‘Items list’, ‘cf’ ),

‘items_list_navigation’      => __( ‘Items list navigation’,’cf’),

);

$args = array(

‘labels’                     => $labels,

‘hierarchical’               => true,

‘public’                     => true,

‘show_ui’                    => true,

‘show_admin_column’          => true,

‘show_in_nav_menus’          => true,

‘show_tagcloud’              => true,

‘show_in_rest’               => true,

‘meta_box_cb’                                                     =>           array($this,’drop_cat’)

);

register_taxonomy( ‘resort_country’, array( ‘resort’ ), $args );

}

Now we have created categories for our custom post type. Just check my code bit deeply you will understand what is going on.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s