How to Create Custom Post Types in WordPress

What is Custom Post Type in WordPress?

Custom post types are content types like posts and pages. Since WordPress evolved from a simple blogging platform into a robust CMS, the term post stuck to it. However, a post type can be any kind of content. By default, WordPress comes with these post types:
  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu
<?php 
 function register_post_story() { 
   $labels = array(  
      'name'  => _x( 'Stories', 'Post Type General Name', 'yoga' ),  
      'singular_name'    => _x( 'Story', 'Post Type Singular Name', 'yoga' ),  
      'menu_name'        => __( 'Stories', 'yoga' ),  
      'name_admin_bar'        => __( 'Story', 'yoga' ),  
      'archives'              => __( 'Story Archives', 'yoga' ),  
      'parent_item_colon'     => __( 'Parent Story:', 'yoga' ),  
      'all_items'             => __( 'All Stories', 'yoga' ),  
      'add_new_item'          => __( 'Add New Story', 'yoga' ),  
      'add_new'               => __( 'Add Story', 'yoga' ),  
      'new_item'              => __( 'New Story', 'yoga' ),  
      'edit_item'             => __( 'Edit Story', 'yoga' ),  
      'update_item'           => __( 'Update Story', 'yoga' ),  
      'view_item'             => __( 'View Story', 'yoga' ),  
      'search_items'          => __( 'Search Story', 'yoga' ),  
      'not_found'             => __( 'Not found', 'yoga' ),  
      'not_found_in_trash'    => __( 'Not found in Trash', 'yoga' ),  
      'featured_image'        => __( 'Story Image', 'yoga' ),  
      'set_featured_image'    => __( 'Set story image', 'yoga' ),  
      'remove_featured_image' => __( 'Remove story image', 'yoga' ),  
      'use_featured_image'    => __( 'Use as story image', 'yoga' ),  
      'insert_into_item'      => __( 'Insert into story', 'yoga' ),  
      'uploaded_to_this_item' => __( 'Uploaded to this story', 'yoga' ),  
      'items_list'            => __( 'Stories list', 'yoga' ),  
      'items_list_navigation' => __( 'Stories list navigation', 'yoga' ),  
      'filter_items_list'     => __( 'Filter stories list', 'yoga' ), 
      ); 
   $args = array(  
     'label'  => __( 'Story', 'yoga' ),  
     'description'           => __( 'Stories', 'yoga' ),  
     'labels'                => $labels,  
     'supports'              => array( 
             'title', 
             'editor', 
             'excerpt', 
             'author', 
             'thumbnail', 
             'revisions', 
             'custom-fields',
            ), 
     'taxonomies'            => array( 
             'story-category' 
            ),  
     'hierarchical'          => false,  
     'public'                => true,  
     'show_ui'               => true,  
     'show_in_menu'          => true,  
     'menu_position'         => 5,  
     'menu_icon'             => 'dashicons-editor-quote',  
     'show_in_admin_bar'     => true,  
     'show_in_nav_menus'     => true,  
     'can_export'            => true,  
     'has_archive'           => true,    
     'exclude_from_search'   => false,  
     'publicly_queryable'    => true,  
     'capability_type'       => 'page', 
   ); 
  register_post_type( 'story', $args );
 }
 add_action( 'init', 'register_post_story', 0 );

Share this

Related Posts

Previous
Next Post »