Swatist
2/21/2017 - 12:52 PM

how to create Dynamic Custom Post type

how to create Dynamic Custom Post type


/**
 * create new cpt
 */

  // Our custom post type function
function create_posttype_portfolio() {
   register_post_type( 'portfolio',
 // CPT Options
  array(
   'labels' => array(
    'name' => __( 'portfolio' ),
    'singular_name' => __( 'portfolio' )
   ),
   'public' => true,
   'has_archive' => true,
   'rewrite' => array('slug' => 'portfolio'),
  )
 );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype_portfolio' );
/*
* Creating a function to create our CPT
*/
function custom_post_type() {

// Set UI labels for Custom Post Type
 $labels = array(
  'name'                => _x( 'portfolio', 'Post Type General Name', 'portfolio' ),
  'singular_name'       => _x( 'portfolio', 'Post Type Singular Name', 'portfolio' ),
  'menu_name'           => __( 'portfolio', 'portfolio' ),
  'parent_item_colon'   => __( 'Parent portfolio', 'portfolio' ),
  'all_items'           => __( 'All portfolio', 'portfolio' ),
  'view_item'           => __( 'View portfolio', 'portfolio' ),
  'add_new_item'        => __( 'Add New portfolio', 'portfolio' ),
  'add_new'             => __( 'Add New', 'portfolio' ),
  'edit_item'           => __( 'Edit portfolio', 'portfolio' ),
  'update_item'         => __( 'Update portfolio', 'portfolio' ),
  'search_items'        => __( 'Search portfolio', 'portfolio' ),
  'not_'           => __( 'Not ', 'portfolio' ),
  'not__in_trash'  => __( 'Not  in Trash', 'portfolio' ),
 );
 
// Set other options for Custom Post Type
 
 $args = array(
  'label'               => __( 'portfolio', 'portfolio' ),
  'description'         => __( 'portfolio news and reviews', 'portfolio' ),
  'labels'              => $labels,
  // Features this CPT supports in Post Editor
  'supports'            => array( 'title',  'thumbnail', 'gallery'),
  // You can associate this CPT with a taxonomy or custom taxonomy. 
  'taxonomies'          => array( 'genres','category','post_tag'),
  /* 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'     => 'page',
 );
 
 // Registering your Custom Post Type
 register_post_type( 'portfolio', $args );

}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
add_action( 'init', 'custom_post_type', 0 );