In this blog we will show you how to Create a Custom Post Type in WordPress in few minutes.
There are 2 ways you can add custom post type to your WordPress theme. In which you can add custom post type with plugins and without plugins. In this blog, I will show you how to add a custom post type so that you can create a custom post type for your website.
How to Create a Custom Post Type in WordPress Without a Plugin
To create a custom post type in WordPress without a plugin, you need to use your functions.php
I will show you how to create a custom post type on news. You will also see Custom News Category.
Add the following code to functions.php
/* --
Custom post type and taxonomy for news
-- */
function create_news_function(){
$labels = array(
'name' => _x('news', 'post type general name', 'your_text_domain'),
'singular_name' => _x('news', 'post type Singular name', 'your_text_domain'),
'add_new' => _x('Add new', '', 'your_text_domain'),
'add_new_item' => __('Add New news', 'your_text_domain'),
'edit_item' => __('Edit news', 'your_text_domain'),
'new_item' => __('New news', 'your_text_domain'),
'all_items' => __('All News Post', 'your_text_domain'),
'view_item' => __('View news', 'your_text_domain'),
'search_items' => __('Search news', 'your_text_domain'),
'not_found' => __('No news found', 'your_text_domain'),
'not_found_in_trash' => __('No news on trash', 'your_text_domain'),
'parent_item_colon' => '',
'menu_name' => __('news', 'your_text_domain')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'menu_icon' => 'dashicons-welcome-write-blog',
'supports' => array('title','editor', 'thumbnail')
);
register_post_type('news', $args);
flush_rewrite_rules();
}
add_action('init', 'create_news_function');
How to add a custom category in a custom post type
Below is the code to create a custom category in a custom post type
Add the following code to functions.php
$labels = array(
'name' => __('News Category'),
'singular_name' => __('Category'),
'search_items' => __('Search'),
'popular_items' => __('More Used'),
'all_items' => __('All News Categories'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Add new'),
'update_item' => __('Update'),
'add_new_item' => __('Add new Category'),
'new_item_name' => __('New')
);
register_taxonomy('news_category', array('news'), array(
'hierarchical' => true,
'labels' => $labels,
'singular_label' => 'news_category',
'all_items' => 'Category',
'query_var' => true,
'rewrite' => array('slug' => 'cate'))
);
How to Create a Custom Post Type in WordPress With a Plugin
Here I will use the custom-post-type-ui plugin to create custom post type in WordPress with a plugin. Apart from this there are some other plugins that you can use to create custom post type.
Install the plugin and CTP UI will be added to the dashboard.
Now you can create any post type you want.
Location on using this produce-up, I honestly believe this site demands far more consideration. I’ll possibly be back again all over again to determine additional, many thanks for the knowledge!
Thank You Lamont Sears