How to Add Custom Post Types in WordPress

WordPress’s custom post type allows you to create your own version of default ‘post’ or ‘page’.

We usually write blog posts using the “Post” custom post type or even blog pages with “Page” post type.

So what if you want to create your own post type, like you want a separate one for ‘Portfolio’ like what I did on this site.

That’s when creating your own custom post type makes the most sense.

Once you’ve successfully created it, it will render on the sidebar when you’re logged in. Usually it’s below “Comments” menu.

You just have to add this code to your functions.php or if you are working on a plugin, you could write this on your-plugin-name.php.

/**
 * This is the function that's being called on
 * WordPress init where register_post_type is being called.
 */


function portfolio_post_type(){

    /**
     * $args refers to the options we can
     * add to the post type.
     */

    $args = [
        'labels' => [
            'name' => 'Portfolio',
            'singular_name' => 'Portfolio'
        ],
        'public' => true,
        'has_archive' => true,
        'supports' => [
            'title',
            'editor',
            'thumbnail'
        ]
    ];


    /**
     * This is a WordPress reserved function to add a 
     * custom_post_type
     */


    register_post_type('portfolio', $args);
};


/**
 * Call the function that registers the custom post type
 * right on WordPress initialization
 */


add_action('init', 'portfolio_post_type');

Leave a Reply

Your email address will not be published. Required fields are marked *