2 mins read
Add A WordPress Custom Post Type with Custom Categories
/*
* Creating a function to create our CPT
*/
function custom_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', 'puppies' ),
'parent_item_colon' => __( 'Parent Puppy', 'puppies' ),
'all_items' => __( 'All Puppies', 'puppies' ),
'view_item' => __( 'View Puppy', 'puppies' ),
'add_new_item' => __( 'Add New Puppy', 'puppies' ),
'add_new' => __( 'Add New', 'puppies' ),
'edit_item' => __( 'Edit Puppy', 'puppies' ),
'update_item' => __( 'Update Puppy', 'puppies' ),
'search_items' => __( 'Search Puppy', 'puppies' ),
'not_found' => __( 'Not Found', 'puppies' ),
'not_found_in_trash' => __( 'Not found in Trash', 'puppies' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Puppies', 'puppies' ),
'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( 'breeds' ),
/* 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' => true,
);
// 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', 'custom_post_type', 0 );
// END CUSTOM POST TYPE
//Register Custom Category
register_taxonomy( 'breeds',
array('breeds'),
array('hierarchical' => true,
'labels' => array(
'name' => __( 'Breeds', 'puppies' ),
'singular_name' => __( 'Breed', 'puppies' ),
'search_items' => __( 'Search Breeds', 'puppies' ),
'all_items' => __( 'All Breeds', 'puppies' ),
'parent_item' => __( 'Parent Breed', 'puppies' ),
'parent_item_colon' => __( 'Parent Breed:', 'puppies' ),
'edit_item' => __( 'Edit Breed', 'puppies' ),
'update_item' => __( 'Update Breed', 'puppies' ),
'add_new_item' => __( 'Add New Breed', 'puppies' ),
'new_item_name' => __( 'New Breed Category Name', 'puppies' )
),
'show_admin_column' => true,
'show_ui' => true,
'query_var' => true,
'public' => true,
'rewrite' => array( 'slug' => 'breeds' ),
)
);
