Add custom post type in wordpress
Let’s say you would like to add custom post type in wordpress site for your products. let’s call your new custom wordpress post type ‘our-products’.
- Add the following to your theme’s functions.php file
function create_post_type() {
register_post_type( ‘our-products’ , array( ‘labels’ => array(‘name’ =>_( ‘Products’ ),’singular_name’ => _ ( ‘Product’ )),’public’=>true, ‘hierarchical’ => true, ‘show_ui’=>true, ‘taxonomies’ => array(‘post_tag’) , ‘supports’ => array(‘title’,'editor’,'thumbnail’,'excerpt’, ‘tags’, ‘page-attributes’, ‘custom-fields’)));
register_taxonomy_for_object_type( ‘post_tag’ , ‘Products’ );
}
add_action( ‘init’, ‘create_post_type’ );
register_post_type( ‘our-products’ , array( ‘labels’ => array(‘name’ =>_( ‘Products’ ),’singular_name’ => _ ( ‘Product’ )),’public’=>true, ‘hierarchical’ => true, ‘show_ui’=>true, ‘taxonomies’ => array(‘post_tag’) , ‘supports’ => array(‘title’,'editor’,'thumbnail’,'excerpt’, ‘tags’, ‘page-attributes’, ‘custom-fields’)));
register_taxonomy_for_object_type( ‘post_tag’ , ‘Products’ );
}
add_action( ‘init’, ‘create_post_type’ );
- .Now check your wordpress admin. You should see the “Products” post type in there. You can add ‘products’ by clicking the ‘add new product’.
You will also notice that you have other fields such as expert, tags, page-attributes, custom fields, featured image etc in your new custom wordpress post type.
This is because we have enabled them in the finctions.php file with the above code.
‘supports’ => array(‘title’,'editor’,'thumbnail’,'excerpt’, ‘tags’, ‘page-attributes’, ‘custom-fields’
- If post thumbnails / featured image is not appearing in your theme, make sure you have the following code snippet in your theme’s functions.php file.
if ( function_exists( ‘add_theme_support’ ) ) {
add_theme_support( ‘post-thumbnails’ );
}
add_theme_support( ‘post-thumbnails’ );
}