Skip to content

WordPress: Add Custom Post Type

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.

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');

2 Comments

Leave a Comment

Your email address will not be published.