The power and flexibility of WordPress never ceases to amaze! Today we are going to show you how to create something called a “custom post type”. Custom post types are an additional “type” of “post” and are very handy if you need to create an extra section on your website. For example if you have products that you would like to add and you created a custom post type called “Products” the url would be http://www.yourwebsite/products/example.
Creating a custom post type in WordPress is extremely simple and just requires you to copy and paste the below code into your functions.php file. Remember to replace the word “product” with whatever you want to call your custom post type.
This example enables support for: excerpt, custom-fields, thumbnails
add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'products', array( 'labels' => array( 'name' => __( 'Products' ), 'singular_name' => __( 'Products' ), ), 'menu_position' => 4, 'rewrite' => array('slug'=>'','with_front'=>false), 'public' => true, 'has_archive' => true, 'taxonomies' => array('category', 'post_tag'), 'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ), ) ); }
If you want to have multiple post types the code should look something like this (remember to replace “Products” & “Testimonials” with whatever you want to call your custom post types):
function create_post_type() { register_post_type( 'products', array( 'labels' => array( 'name' => __( 'Products' ), 'singular_name' => __( 'Products' ), ), 'menu_position' => 4, 'rewrite' => array('slug'=>'','with_front'=>false), 'public' => true, 'has_archive' => true, 'taxonomies' => array('category', 'post_tag'), 'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ), ) ); register_post_type( 'testimonials', array( 'labels' => array( 'name' => __( 'Testimonials' ), 'singular_name' => __( 'Testimonials' ), ), 'menu_position' => 5, 'rewrite' => array('slug'=>'','with_front'=>false), 'public' => true, 'has_archive' => true, 'taxonomies' => array('category', 'post_tag'), 'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ), ) ); }