Register a hierarchical custom taxonomy with full labels, REST API support, and admin column. Attach it to any custom post type.
Back to Snippets
php
• 25 lines
PHP
Register a Custom Post Type with REST API Support
Register a custom post type with full labels, REST API support, archive page, and featured image. Production-ready with proper i18n.
Snippet Stats
Lines
25
Characters
1,074
Read
1 min
/**
* Register 'portfolio' custom post type.
*
* @return void
*/
function prefix_register_portfolio_cpt(): void {
register_post_type( 'portfolio', array(
'labels' => array(
'name' => __( 'Portfolio', 'theme-domain' ),
'singular_name' => __( 'Project', 'theme-domain' ),
'add_new_item' => __( 'Add New Project', 'theme-domain' ),
'edit_item' => __( 'Edit Project', 'theme-domain' ),
'all_items' => __( 'All Projects', 'theme-domain' ),
'search_items' => __( 'Search Projects', 'theme-domain' ),
'not_found' => __( 'No projects found.', 'theme-domain' ),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'portfolio' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'menu_icon' => 'dashicons-portfolio',
) );
}
add_action( 'init', 'prefix_register_portfolio_cpt' );
Found an issue with this snippet? Help us improve by reporting it. Report it →