CREATE CUSTOM POST TYPE IN WP THEME

Hi guys,
here is the code snippet to create custom post type in wp theme

to create custom post type in wp theme, add the following code to your theme’s functions.php file.

function ucs_custom_post_products() {
    $labels = array(
        'name' => _x('Products', 'post type general name', 'ucs'),
        'singular_name' => _x('Products', 'post type singular name', 'ucs'),
        'add_new' => _x('Add New Products', 'book', 'ucs'),
        'add_new_item' => __('Add New Products', 'ucs'),
        'edit_item' => __('Edit Products', 'ucs'),
        'new_item' => __('New Products', 'ucs'),
        'view_item' => __('View Products', 'ucs'),
        'search_items' => __('Search Products', 'ucs'),
        'not_found' =>  __('No Products found', 'ucs'),
        'not_found_in_trash' => __('No Products found in Trash', 'ucs'), 
        'parent_item_colon' => ''
    );      
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail'),
        'menu_icon' => 'dashicons-cart'
    );      

    register_post_type( 'products', $args );

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

    register_taxonomy(
        'productscats',
        'product',
        array(
            'public'=>true,
            'hierarchical' => true,
            'labels'=> $labels,
            'query_var' => 'productscats',
            'show_ui' => true,
            'rewrite' => array( 'slug' => 'productscats', 'with_front' => false ),
        )
    );        
}                                  
add_action('init', 'ucs_custom_post_products');

Thank You.

Leave a comment