How to create custom post type in wordpress??

Do you want to create different post type like posts and pages in wordpress?.

You can simply create a custom post type in your wordpress theme by adding a code in function.php file. Follow the below code for create custom posts:-

In this example we will create Service Post type . You can rename your post type as you required. Hence, Go to your wordpress theme folder and open function file.

WordPress theme folder>> function.php

Paste the code shown in below:-

function custom_post_services_type() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Services', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Services', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Services', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Services', 'twentythirteen' ),
        'all_items'           => __( 'All Services', 'twentythirteen' ),
        'view_item'           => __( 'View Services', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Services', 'twentythirteen' ),
        'add_new'             => __( 'Add Services', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Services', 'twentythirteen' ),
        'update_item'         => __( 'Update Services', 'twentythirteen' ),
        'search_items'        => __( 'Search Services', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );
     
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'Services', 'twentythirteen' ),
        'description'         => __( 'Services  and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor','thumbnail', 'custom-fields' ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'has_archive'         => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 6,
        'can_export'          => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
     
    // Registering your Custom Post Type
    register_post_type( 'services', $args );
    flush_rewrite_rules( false );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 add_action( 'init', 'custom_post_services_type', 0 );

It will display your post type in dashboard. Add your post content in your post type by clicking on it.

Now the problem arieses how to display custom post in your template php file? It’s as simple as creating the code for post type. Copy the code shown in below and paste in your template file or where you want to display your post content.

<?php $args = array(
    'post_type' => 'services' ,// your post type name
     'order' => 'DESC' ,    // ACS or DESC
   'posts_per_page' => 4, // Number of post you want to display in page
           ); ?>
            <?php query_posts($args); ?>
          <?php if ( have_posts() ) : ?>
       <?php while ( have_posts() ) : the_post();?>
                               // your code here
             <?php endwhile;?>
           <?php endif;?>