Setting Up Your Post Thumbnail Size in WordPress: A Step-by-Step Guide

Home » Snippets » Setting Up Your Post Thumbnail Size in WordPress: A Step-by-Step Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Working with images in WordPress is relatively straight forward, but when it comes to setting the thumbnail size, it can seem a bit tricky. Don’t worry – we’re here to help! In this article, we will guide you on how to effectively set the post thumbnail size for your WordPress site. Whether you’re an experienced developer or a beginner, you’ll learn how to effortlessly adjust thumbnails to suit your website’s aesthetic and layout. By the end of this guide, your thumbnail sizes will no longer be an obstacle, but an aspect of your site you can seamlessly control.

					function wpturbo_set_post_thumbnail_size() {
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 1200, 9999 ); // 1200 pixels wide by 9999 pixels unlimited height, soft proportional crop mode
}
add_action( 'after_setup_theme', 'wpturbo_set_post_thumbnail_size' );
				

The above code is all about setting a specific size to all post thumbnails in your WordPress theme. We can do this via the set_post_thumbnail_size() function, which is a part of core WordPress functionality to manipulate media settings.

The first line of the code defines a function wpturbo_set_post_thumbnail_size(). This custom function will be responsible for setting the post thumbnail size.

function wpturbo_set_post_thumbnail_size() {

Inside this function, we first call add_theme_support( 'post-thumbnails' ); to enable featured image support for your theme. This line is essential because without it, the Post Thumbnail feature will not be enabled,

Next, we make use of the function set_post_thumbnail_size( 1200, 9999 ), to set the post thumbnail’s size dimensions. The numbers 1200 and 9999 represent width and height, respectively. The width is set to 1200 pixels while the height is set to 9999 pixels.

    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 1200, 9999 ); // 1200 pixels wide by 9999 pixels unlimited height, soft proportional crop mode

This effectively sets a proportional width of 1200 pixels for post thumbnails, but an essentially unlimited height (up to 9999 pixels). As a result, when a post thumbnail gets generated, WordPress will attempt to maintain the aspect ratio of the uploaded image, with a maximum width of 1200 pixels. But it will also ensure the image can be as tall as it needs to be to maintain that aspect ratio, up to a maximum height of 9999 pixels. The ‘soft proportional crop mode’ comment indicates this behavior.

And finally, this function is hooked into the WordPress lifecycle via the add_action('after_setup_theme', 'wpturbo_set_post_thumbnail_size') function. The after_setup_theme hook fires after the theme is loaded and its functions file (functions.php) is read, which is the perfect timing to declare theme support for post thumbnails and set the thumbnail size.

add_action( 'after_setup_theme', 'wpturbo_set_post_thumbnail_size' );

Register an account to save your snippets or go Pro to get more features.