Showing posts with label custom post type code. Show all posts
Showing posts with label custom post type code. Show all posts

Friday, April 21, 2023

Create custom post type in wordpress with code and without plugin

Hello Today I am going to explain how to create custom post type in any wordpress theme. No need for plugin. Simply copy paste this code in your current theme->functions.php file.


functions.php

//custom post type for Consultant details
function consultatns_custom() {
  
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Consultant', 'Post Type General Name', 'trek' ),
        'singular_name'       => _x( 'consultant', 'Post Type Singular Name', 'trek' ),
        'menu_name'           => __( 'Consultant', 'trek' ),
        'parent_item_colon'   => __( 'Parent Consultant', 'trek' ),
        'all_items'           => __( 'All Consultants', 'trek' ),
        'view_item'           => __( 'View consultant', 'trek' ),
        'add_new_item'        => __( 'Add New consultant', 'trek' ),
        'add_new'             => __( 'Add New', 'trek' ),
        'edit_item'           => __( 'Edit consultant', 'trek' ),
        'update_item'         => __( 'Update consultant', 'trek' ),
        'search_items'        => __( 'Search consultant', 'trek' ),
        'not_found'           => __( 'Not Found', 'trek' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'trek' ),
    );
      
// Set other options for Custom Post Type
      
    $args = array(
        'label'               => __( 'consultant', 'trek' ),
        'description'         => __( 'All Consultant data', 'trek' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', '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,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
        
        'taxonomies'          => array( 'category' ),
    );
      
    // Registering your Custom Post Type
    register_post_type( 'consultant', $args );
  
}
  
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
  
add_action( 'init', 'consultatns_custom', 5 );