Site icon Hip-Hop Website Design and Development

How To Create Custom Post Type And Taxonomy in WordPress

//Code For Cusom Post Type And Taxonomy :
// Car Post Type
function car_init() {
    // set up product labels
    $labels = array(
        'name' => 'Cars',
        'singular_name' => 'Cars',
        'add_new' => 'Add New Car',
        'add_new_item' => 'Add New Car',
        'edit_item' => 'Edit Car',
        'new_item' => 'New Car',
        'all_items' => 'All Cars',
        'view_item' => 'View Car',
        'search_items' => 'Search Cars',
        'not_found' =>  'No Cars Found',
        'not_found_in_trash' => 'No Cars found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Cars',
    );
    
    // register post type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'cars'),
        'query_var' => true,
        'menu_icon' => 'dashicons-randomize',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes'
        )
    );
    register_post_type( 'cars', $args );
    
 //company taxonomy   
// register taxonomy
    register_taxonomy('company', 'cars', array('hierarchical' => true, 'label' => 'Company', 'query_var' => true, 'rewrite' => array( 'slug' => 'model' )));
}
add_action( 'init', 'car_init' );

Note:Put This Code In functions.php
I hope this will help 🙂