When building a custom WordPress theme, you often need to manage content that doesn’t fit into the standard ‘Post’ or ‘Page’ structure. This is where Custom Post Types (CPTs) come in. They allow you to define your own content types with their own set of fields and templates. Here’s the basic code to register a new ‘Portfolio Item’ post type, which you can place in your theme’s functions.php file.
WordPress: Add Custom Post Type
function create_custom_post_type() {
register_post_type('portfolio_item', [
'labels' => [
'name' => 'Portfolio',
'singular_name' => 'Portfolio Item',
],
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'thumbnail'],
]);
}
add_action('init', 'create_custom_post_type');
Exactly what I was looking for. Thanks!
Super helpful snippet for my new project.