Register a Custom Post Type
A ready-to-edit example that registers a "Portfolio" post type with archive, featured images and block editor support.
Opens WPIDE on
Code
add_action( 'init', function () {
register_post_type( 'portfolio', array(
'labels' => array(
'name' => 'Portfolio',
'singular_name' => 'Portfolio Item',
'add_new_item' => 'Add New Portfolio Item',
'edit_item' => 'Edit Portfolio Item',
'all_items' => 'All Portfolio Items',
'search_items' => 'Search Portfolio',
'not_found' => 'No portfolio items found.',
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_icon' => 'dashicons-portfolio',
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
'rewrite' => array( 'slug' => 'portfolio' ),
) );
} );
Notes
After activating, visit Settings > Permalinks once so WordPress refreshes its URL rules.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.