1 min read
Add New Custom Post Type to WordPress via Functions.php File
This will create a custom post type call Puppies and will have a non-hierarchical like standard WordPress Posts
/*
* Creating a function to create our CPT
*/
function puppies_post_type() {
// // Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Puppies', 'Post Type General Name', 'puppies'),
'singular_name' => _x( 'Puppy', 'Post Type Singular Name', 'puppies'),
'menu_name' => __( 'Puppies', 'theme-name'),
'parent_item_colon' => __( 'Parent Puppy', 'theme-name'),
'all_items' => __( 'All Puppies', 'theme-name'),
'view_item' => __( 'View Puppy', 'theme-name'),
'add_new_item' => __( 'Add New Puppy', 'theme-name'),
'add_new' => __( 'Add New', 'theme-name'),
'edit_item' => __( 'Edit Puppy', 'theme-name'),
'update_item' => __( 'Update Puppy', 'theme-name'),
'search_items' => __( 'Search Puppy', 'theme-name'),
'not_found' => __( 'Not Found', 'theme-name'),
'not_found_in_trash' => __( 'Not found in Trash', 'theme-name'),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Puppies', 'theme-name'),
'description' => __( 'Puppies For Sale or Adoption', 'puppies'),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'categories', 'tags' , 'custom-taxomony' ),
/* 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,
'menu_icon' => 'dashicons-pets',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => false,
);
// Registering your Custom Post Type
register_post_type( 'puppies', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'puppies_post_type', 0 );
// END CUSTOM POST TYPE
