Creating WordPress Custom Post Type

Creating 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’)

Creating Custom Post Type

Suppose we 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 we can create a Custom Post Type named ‘service’ which 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 we made ‘service’). 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 wordpress CMS (here ‘Services’). See all the parameters of register_post_type at – https://codex.wordpress.org/Function_Reference/register_post_type

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 here we have created custom post type ‘service’ so we need to create two templates in our wordpress theme which 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

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.