function register_custom_post_type()
{
$labels = [
'name' => __('Code Library', 'Code Library', 'twentytwentyone'),
'singular_name' => __('Snippet', 'Snippet', 'twentytwentyone'),
'menu_name' => __('Code Library', 'twentytwentyone'),
'all_items' => __('All Snippets', 'twentytwentyone'),
'view_item' => __('View Snippet', 'twentytwentyone'),
'add_new_item' => __('Add New Code Snippet', 'twentytwentyone'),
'add_new' => __('Add New', 'twentytwentyone'),
'edit_item' => __('Edit Snippet', 'twentytwentyone'),
'update_item' => __('Update Snippet', 'twentytwentyone'),
'search_items' => __('Search Snippets', 'twentytwentyone'),
'not_found' => __('Not Found', 'twentytwentyone'),
'not_found_in_trash' => __('Not found in Trash', 'twentytwentyone'),
];
register_post_type(
'code-library',
[
'labels' => $labels,
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'code-library'],
'show_in_rest' => true,
'menu_icon' => 'dashicons-editor-code',
'supports' => [
'title',
'editor',
'excerpt'
]
]
);
register_taxonomy(
'language',
'code-library',
[
'hierarchical' => true,
'label' => 'Programming Language',
'query_var' => true,
'rewrite' => [
'slug' => 'language',
'with_front' => false
],
'show_in_rest' => true,
]
);
}
add_action('init', 'register_custom_post_type');
Here’s an example of how I created the CPT and custom taxonomy for the post you are reading now. While I can’t bring myself to use the old school array()
syntax that the documentation advises, I don’t deviate very far from the recommended syntax.