To add a new custom post type and a custom taxonomy in WordPress, you need to follow these steps:
**Step 1: Register a Custom Post Type:**
In this example, let’s create a custom post type called “Books.”
function custom_book_post_type() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'menu_name' => 'Books', 'name_admin_bar' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'view_item' => 'View Book', 'all_items' => 'All Books', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in trash', ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'books'), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'), ); register_post_type('book', $args); } add_action('init', 'custom_book_post_type');
**Step 2: Register a Custom Taxonomy:**
In this example, let’s create a custom taxonomy called “Genres” and associate it with the “Books” custom post type.
function custom_book_genres_taxonomy() { $labels = array( 'name' => 'Genres', 'singular_name' => 'Genre', 'search_items' => 'Search Genres', 'popular_items' => 'Popular Genres', 'all_items' => 'All Genres', 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => 'Edit Genre', 'update_item' => 'Update Genre', 'add_new_item' => 'Add New Genre', 'new_item_name' => 'New Genre Name', 'separate_items_with_commas' => 'Separate genres with commas', 'add_or_remove_items' => 'Add or remove genres', 'choose_from_most_used' => 'Choose from the most used genres', 'not_found' => 'No genres found.', 'menu_name' => 'Genres', ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'genre'), ); register_taxonomy('genre', 'book', $args); } add_action('init', 'custom_book_genres_taxonomy');
With these code snippets, you have created a new custom post type “Books” and a custom taxonomy “Genres” associated with the “Books” post type. Now you can go to your WordPress dashboard and see the new “Books” menu item, where you can add and manage your book entries. Additionally, you can assign genres to each book using the “Genres” taxonomy. Remember to customize the labels, slugs, and parameters according to your requirements.