Create Custom Post Type with Texamony(Categories) in WordPress

Custom Post types are very handy tool in wordpress. To create a custom Post Type is pretty easy.

Goto functions.php file of your theme folder and copy the below code. You will get the new section for adding posts. These can be used for many stuffs like creating a small plugin for News, FAQ’s etc.

Below is the code to be added in functions.php. I have added the comment wherever required.

wp-custom-post

/* Custom Post Type */

add_action(‘init’, ‘banner_slider’); //inits the functions below and call it.

function banner_slider() {
$speaker_labels = array(
‘name’                  => _x(‘Banner Slider’, ‘post type general name’, GCT_NAME),
‘singular_name’         => _x(‘Banner Slider’, ‘post type singular name’, GCT_NAME),
‘add_new’               => __(‘Add New’, GCT_NAME),
‘add_new_item’          => __(‘Add New Slide’, GCT_NAME),
‘edit_item’             => __(‘Edit Slide’, GCT_NAME),
‘new_item’              => __(‘New Slide’, GCT_NAME),
‘all_items’             => __(‘All Slide’, GCT_NAME),
‘view_item’             => __(‘View Slide’, GCT_NAME),
‘search_items’          => __(‘Search Slide’, GCT_NAME),
‘not_found’             => __(‘Nothing Found’, GCT_NAME),
‘not_found_in_trash’    => __(‘Nothing found in Trash’, GCT_NAME),
‘parent_item_colon’     => ”
);

$speaker_args = array(
‘labels’                => $speaker_labels,
‘singular_label’        => __(‘slider’, GCT_NAME),
‘public’                => true,
‘show_ui’               => true,
‘capability_type’       => ‘post’,
‘rewrite’               => true,
‘menu_icon’             => ‘dashicons-businessman’,
‘menu_position’         => 20,
‘exclude_from_search’   => true,
‘map_meta_cap’ => true,
‘hierarchical’ => true,
‘rewrite’ => array(‘slug’ => ‘slider’, ‘with_front’ => true),
‘query_var’ => ‘slider’,
‘has_archive’ => true,
‘supports’ => array(‘title’, ‘excerpt’, ‘editor’, ‘thumbnail’)
);

register_post_type(‘slider’,$speaker_args);
flush_rewrite_rules();

/* Now to categorise the custome post type you need to add a texamony to it. This can be done by adding the below code : */

$categories_labels = array(
‘name’                  => __(‘Slider Categories’, GCT_NAME),
‘singular_name’         => _x(‘Category’, GCT_NAME),
‘all_items’             => __(‘All Categories’, GCT_NAME),
‘add_new_item’          => _x(‘Add New Category’, GCT_NAME),
‘edit_item’             => __(‘Edit Category’, GCT_NAME),
‘new_item’              => __(‘New Category’, GCT_NAME),
‘view_item’             => __(‘View Category’, GCT_NAME),
‘search_items’          => __(‘Search Category’, GCT_NAME),
‘not_found’             => __(‘Nothing Found’, GCT_NAME),
‘not_found_in_trash’    => __(‘Nothing found in Trash’, GCT_NAME),
‘parent_item_colon’     => ”
);

register_taxonomy(“slider-category”, array(“slider”), array(
“hierarchical” => true,
“labels” => $categories_labels,
“rewrite” => array(“slug” => “”, “hierarchical” => false, “with_front” => false)
));

}

Now opening the backend of wordpress you will see a New section name Banner Slider. Here i have given it the name banner slider as i used it to create a slider.

To call this page/post types you can use the URL slider which is defined as slug above.

Normally custom post types are called from archive.php but if you want to change the structure or layout of how they are displayed, simply create another archive file adding the slug of custom post type.

In our case it will be archive-slider.php. Thats it any change you make in this file will be displayed when you open the website with URL: http://www.example.com/slider

Please post your comments if you have any doubts regarding it.

Advertisements

3 thoughts on “Create Custom Post Type with Texamony(Categories) in WordPress

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s