WordPress: How to Create a Custom Post Type

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' ),
)
);
}
Free Graphics, Icons, Tutorials
Free Graphics Free Christmas Vector Icon Graphics Pack 2017Free Fitness Vector Icons Graphics PackFree Camping Vector Graphics PackFree Summer Graphics PackFree File Icon PackFree Fast Food Vector Graphics
Sharing is caring...
Like & Follow
Share the love, share this page! Facebook Twitter Digg Reddit LinkedIn Pinterest Email
Close [X]
The Web Taylor
1000 Lakeside North Harbour Portsmouth, Hampshire PO6 3EN
02392 123358