Tutorial – How to create WordPress Custom Post Type

Tutorial – How to create WordPress Custom Post Type

WordPress Custom Post Type is a new post type which you can create in your WordPress website. Any number of custom post types can be created depending upon your needs. By default WordPress have 5 post types which are –

  • 1. Post (Post Type: ‘post’)
  • 2. Page (Post Type: ‘page’)
  • 3. Attachment (Post Type: ‘attachment’)
  • 4. Revision (Post Type: ‘revision’)
  • 5. Navigation menu (Post Type: ‘nav_menu_item’)
WordPress developers must also know how to create WordPress Shortcode which help the common users to add great features from the dashboard itself.

Creating Custom Post Type

Suppose you are creating a WordPress website for a web designing company which offers many services like web designing, SEO, email marketing, SEM and website hosting. Here you can create a Custom Post Type called ‘service’, and it will contains all the services of the company.

To do this open your functions.php file and add the following code –

/*Creating Service Custom Post Type*/
add_action( 'init', 'create_post_type_service' );
function create_post_type_service() {
    $post_supports = array('title'
    ,'editor'
    ,'author'
    ,'thumbnail'
    ,'excerpt'
    ,'trackbacks'
    ,'custom-fields'
    ,'comments'
    ,'revisions'
  );
	register_post_type( 'service',
		array(
			'labels' => array(
				'name' => __( 'Services' ),
				'singular_name' => __( 'Service' )
			),
        'taxonomies' => array('servicecategory'),
		'public' => true,
		'has_archive' => true,
		'supports' => $post_supports,
		)
	);
}
/*End*/

The register_post_type takes on 2 arguments. The first one is the name of the custom post type (here I made ‘service’).

Now you don’t have to move to any other email marketing service because WordPress Subscription Plugin can help you to create and send powerful newsletters to your customer’s email addresses.

The second one is an array containing options for giving it name, singular_name, etc. The string, which you give for its name, is the name by which it will display in the dashboard (here ‘Services’).

See all the parameters of register_post_type at – here.

Showing Custom Post Type in the Website

You can create custom templates for your custom post type in order to show your custom posts in the website. In our example I have created custom post type service, so I need to create two templates in our WordPress theme, these are –

  • 1. archive-service.php – It will show ‘service’ custom post type archive.

    All Custom Post Type showing from archive-service.php

    All Custom Post Type showing from archive-service.php

  • 2. single-service.php – It will show single post of our custom post type ‘service’.
    Single Custom Post Type showing from single-service.php

    Single Custom Post Type showing from single-service.php

You may find this tutorial very interesting – How To Change WordPress Header Image?

Conclusion
WordPress Custom Post Type is a great feature to implement in your theme. It is not only easy but also helps you to manage your posts effectively.

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.