WP Custom Post Type Registration
Revision History
No revision history recorded yet.
<?php
add_action( 'init', 'register_book_cpt' );
function register_book_cpt() {
$labels = [
'name' => __( 'Books', 'textdomain' ),
'singular_name' => __( 'Book', 'textdomain' ),
'add_new_item' => __( 'Add New Book', 'textdomain' ),
'edit_item' => __( 'Edit Book', 'textdomain' ),
'search_items' => __( 'Search Books', 'textdomain' ),
'not_found' => __( 'No books found.', 'textdomain' ),
'menu_name' => __( 'Books', 'textdomain' ),
];
register_post_type( 'book', [
'labels' => $labels,
'public' => true,
'has_archive' => true,
'rewrite' => [ 'slug' => 'books' ],
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ],
'show_in_rest' => true,
'menu_icon' => 'dashicons-book',
] );
register_taxonomy( 'genre', 'book', [
'labels' => [ 'name' => __( 'Genres', 'textdomain' ) ],
'hierarchical' => true,
'show_in_rest' => true,
'rewrite' => [ 'slug' => 'genre' ],
] );
}