Skip to Content

How do I enable featured photos?

How do I enable featured photos?

Enabling featured photos on your website can help draw attention to important content and make your site more visually appealing. Featured photos (also sometimes called post thumbnails) are images that are assigned to display with a specific blog post, page, or custom post type. When enabled, WordPress will automatically generate smaller thumbnail images of the featured image to use in archive pages, the blog roll, etc. Here’s what you need to know about using featured images.

What are the requirements for featured images?

There are a few requirements in order to use featured images on your WordPress site:

  • Your theme must support featured images. Most modern WordPress themes include featured image support, but older themes may not. Check with your theme author if you’re not sure.
  • You must be using WordPress version 2.9 or higher. Featured image support was added in WordPress 2.9.
  • The post type being used must support thumbnails. Posts and pages support thumbnails by default. For custom post types, you must enable thumbnail support when registering the post type.

As long as your theme and WordPress version meet the requirements, enabling featured images is easy.

How to enable featured images in WordPress

To enable featured images sitewide:

  1. Go to your dashboard and navigate to Settings > Media.
  2. Find the “Featured Image” section and check the box to enable featured images.
  3. Click the Save Changes button at the bottom of the page.

That’s it! Featured images are now enabled and ready to use.

Enabling featured images for specific post types

In addition to sitewide, you can enable featured images for individual post types. This is handy if you only want thumbnails for some post types but not others. Here’s how:

  1. Go to Functions.php located in your active theme’s folder.
  2. Look for the section where custom post types are registered. It will look something like this:
  3. add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'books',
        array(
          'labels' => array(
            'name' => __( 'Books' ),
            'singular_name' => __( 'Book' )
          ),
        'public' => true,
        'has_archive' => true,
        )
      );
    }
    
  4. To enable featured images, add ‘supports’ => array(‘thumbnail’) to the register_post_type array, like so:
  5. add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'books',
        array(
          'labels' => array(
            'name' => __( 'Books' ),
            'singular_name' => __( 'Book' )
          ),
          'supports' => array('thumbnail'),
        'public' => true,
        'has_archive' => true,
        )
      );
    }  
    
  6. Save functions.php, featured images are now enabled for that post type.
  7. Repeat this process for any other post types you want to enable thumbnails for.

Now that featured images are enabled, it’s time to start adding them to your content!

Adding a featured image to a post or page

Adding a featured image when creating a new post or page is easy:

  1. When creating or editing a post/page, look for the “Featured Image” box on the right side of the screen.
  2. Click “Set featured image.” This will open the media library.
  3. Upload or choose an existing image to use as the featured image.
  4. Click “Set featured image” to assign it to the post.

The selected image now shows up in the featured image box and will display with that post in archive pages, blog rolls, etc.

Setting the featured image via code

You can also programmatically set a post’s featured image with some custom code. Add this to your theme’s functions.php file:

// Set featured image for post
function set_featured_image( $post_id, $image_url ) {

  // Check if post exists
  if ( get_post( $post_id ) ) {

    // Set featured image
    update_post_meta( $post_id, '_thumbnail_id', $image_url ); 
  }

}

Then you can use it like this:

// Assign featured image
$post_id = 123; 
$thumb_id = 456; // Image attachment ID 

set_featured_image( $post_id, $thumb_id );

This will assign the image with attachment ID 456 as the featured image for the post with ID 123.

Displaying a post’s featured image

Once you’ve assigned a featured image to a post, there are a few ways to display it in your theme:

1. the_post_thumbnail()

This template tag displays the post’s featured image at its full size. For example:

  <?php 
  if ( has_post_thumbnail() ) {
    the_post_thumbnail(); 
  } 
  ?>

This checks if the post has a featured image and if so, displays it.

2. the_post_thumbnail(‘thumbnail’)

To display a resized thumbnail instead of the full image, pass the desired image size as a parameter. Common sizes are ‘thumbnail’, ‘medium’, ‘large’, ‘full’. For example:

  <?php 
  the_post_thumbnail('thumbnail'); 
  ?>

Displays the post’s featured image resized to 150×150 pixels (the ‘thumbnail’ size).

3. get_the_post_thumbnail()

This works the same as the_post_thumbnail() but returns the HTML instead of outputting it directly. Useful if you need to store the thumbnail HTML in a variable.

  <?php
  $thumb_html = get_the_post_thumbnail( $post_id, 'full' );
  ?>

4. Custom image sizes

You can generate custom image sizes instead of just using the defaults. For example:

  add_image_size( 'custom-thumb', 220, 180, true ); 

This creates a 220×180 thumbnail image you can use like this:

  the_post_thumbnail( 'custom-thumb' );

Troubleshooting featured images

If featured images don’t seem to be working, here are some troubleshooting tips:

  • Verify support: Make sure your theme and WordPress version support post thumbnails as mentioned earlier.
  • Check permissions: The uploads folder needs proper permissions to upload and resize images. Set to 755 or 775.
  • Regenerate thumbnails: Try regenerating the thumbnail images. Some plugins like Regenerate Thumbnails can handle this.
  • Switch themes: Switch to a default theme like Twenty Twenty to test if featured images work. If so, there may be an issue with your theme’s support.
  • Debug code: Look for errors in wp-content/debug.log or on the front-end when trying to use featured images.

For more help, search the WordPress support forums or contact your theme/plugin developers.

Featured image plugins

There are also several plugins that extend featured image functionality:

Plugin Description
Multiple Featured Images Allow setting multiple featured images per post instead of just one.
Force Regenerate Thumbnails Regenerate thumbnail images for all posts.
Enable Featured Image Allow featured images on custom post types.
Featured Image Column Add featured image column on Posts page.

These plugins provide additional options and flexibility for working with featured images.

Conclusion

In summary, enabling and using featured images is straightforward:

  1. Verify theme and WordPress support.
  2. Enable featured images in Settings > Media.
  3. Enable for custom post types via register_post_type().
  4. Upload an image when creating or editing posts.
  5. Display the image using the_post_thumbnail() or similar functions.
  6. Consider plugins for more advanced featured image options.

Using featured images is an impactful way to add visual interest to your site. With their built-in WordPress support and flexible display options, you can easily spice up your posts in no time.