How to add a featured image to a custom post type in WordPress

To add a featured image to a custom post type in WordPress, you need to add support for the ‘thumbnail’ feature when registering the custom post type.

Follow these steps:

  1. Locate the code in your theme’s functions.php file or a custom plugin file where the custom post type is registered. The custom post type is registered using the register_post_type() function.
  2. Find the supports parameter in the register_post_type() function. It’s an array that contains the features the custom post type supports, such as ‘title’, ‘editor’, and so on.
  3. Add ‘thumbnail’ to the supports array. If the supports parameter doesn’t exist, add it to the register_post_type() function.

Here’s an example of how to add the ‘thumbnail’ feature to a custom post type called ‘my_custom_post_type’:

function my_custom_post_type() {
    $args = array(
        'label' => 'My Custom Post Type',
        'public' => true,
        'supports' => array('title', 'editor', 'thumbnail'), // Add 'thumbnail' to the supports array
        'has_archive' => true,
    );

    register_post_type('my_custom_post_type', $args);
}
add_action('init', 'my_custom_post_type');
  1. Save the changes in the functions.php file or custom plugin file.
  2. If you’re working on a local development environment or a staging site, refresh the WordPress permalinks by going to “Settings” > “Permalinks” in the admin dashboard and clicking the “Save Changes” button. This step is not necessary on a live site.

Now, the featured image option will be available for your custom post type in the WordPress admin dashboard. When you edit or create a new post for your custom post type, you will see the “Featured Image” meta box in the right sidebar, where you can set a featured image for the post.

Remember to always backup your files before making any changes, and test your changes on a staging site or local development environment before applying them to your live site.

Leave a Reply

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