Custom Post Type With Taxonomy

Mar 13 2018
CreepesLab

Custom post type means new post type. custom post type can be added to wordpress via register_post_type() function. You can open your theme folder and function.php and added below code for custom post type.

add_action('init', 'cl_service');

function cl_service() {
    $labels = array(
        'name' => 'Service',
        'singular_name' => 'Service',
        'menu_name' => 'Services',
        'name_admin_bar' => 'Services',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Service',
        'new_item' => 'New Service',
        'edit_item' => 'Edit Service',
        'view_item' => 'View Service',
        'all_items' => 'All Services',
        'search_items' => 'Search Service',
        'parent_item_colon' => 'Parent Service:',
        'not_found' => 'No Service found.',
        'not_found_in_trash' => 'No Service found in Trash.'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'rewrite' => array('slug' => 'cl-services'),
        'has_archive' => true,
        'menu_position' => 21,
        'menu_icon' => 'dashicons-hammer',
        'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields')
    );
    register_post_type('cl_service', $args);
}

Custom taxonomy is a category of custom post type. Open your theme function.php and added below code.

function cl_taxonomies_service() {
    $labels = array(
        'name' => _x('Service Categories', 'taxonomy general name'),
        'singular_name' => _x('Service Category', 'taxonomy singular name'),
        'search_items' => __('Search Service Categories'),
        'all_items' => __('All Service Categories'),
        'parent_item' => __('Parent Service Category'),
        'parent_item_colon' => __('Parent Service Category:'),
        'edit_item' => __('Edit Service Category'),
        'update_item' => __('Update Service Category'),
        'add_new_item' => __('Add New Service Category'),
        'new_item_name' => __('New Service Category'),
        'menu_name' => __('Service Categories'),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy('Service_category', 'cl_service', $args);
}

add_action('init', 'cl_taxonomies_service', 0);
Share: