PHP Developer

Thursday, 9 April 2015

Create Custom Post with all options like Category, Featured image, Excerpt etc. in wordpress

Create Custom Post with all options like Category, Featured image, Excerpt etc. in WordPress


Copy and Paste this code in function.php file. This function create a post of type "servicerequest".

<?php
/*------------------------ Create Custom Post servicerequest START -------------------------------*/

function service_request() {
    $labels = array(
    'name' => 'Services',
    'singular_name' => 'Services',
    'add_new' => 'Add New Services',
    'add_new_item' => 'Add New Services',
    'edit_item' => 'Edit Services',
    'new_item' => 'New Services',
    'all_items' => 'All Services',
    'view_item' => 'View Services',
    'search_items' => 'Search Services',
    'not_found' => 'No Services Found',
    'not_found_in_trash' => 'No Services found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Request Services',
    );
    //register_post_type is use to create new post type.
    //"servicerequest" is a post type

    register_post_type( 'servicerequest', array(
        'labels' => $labels,
        'has_archive' => true,
        'public' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'author', 'revisions', 'post-formats' ),
        'exclude_from_search' => false,
        'capability_type' => 'post',
    )
    );  
}
add_action( 'init', 'service_request' );

function services_taxonomy() { 
    register_taxonomy( 
        'services_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
        'servicerequest',        //post type name
        array( 
            'hierarchical' => true, 
            'label' => 'Services Category',  //Display name in admin Menu
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'servicerequests', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before
            )
        ) 
    ); 

add_action( 'init', 'services_taxonomy');

/*------------------------ Create Custom Post servicerequest START -------------------------------*/
?>

No comments:

Post a Comment