How do I add a custom image size and use it in a theme template?
Asked on Aug 29, 2025
Answer
To add a custom image size in WordPress, you can use the `add_image_size()` function in your theme's `functions.php` file. Once the custom size is registered, you can display it in your theme templates using the `the_post_thumbnail()` function with the custom size name.
<!-- BEGIN COPY / PASTE -->
// Add this to your theme's functions.php
add_action('after_setup_theme', 'my_custom_image_sizes');
function my_custom_image_sizes() {
add_image_size('custom-size', 800, 600, true); // 800px wide by 600px tall, hard crop mode
}
// Use this in your theme template file
if (has_post_thumbnail()) {
the_post_thumbnail('custom-size');
}
<!-- END COPY / PASTE -->Additional Comment:
- Use `add_image_size()` to define custom dimensions and cropping behavior.
- Ensure `after_setup_theme` is used to hook the image size registration, allowing it to load after the theme is set up.
- Regenerate thumbnails using a plugin like "Regenerate Thumbnails" if you add new sizes after images are already uploaded.
- In your template, check for a post thumbnail with `has_post_thumbnail()` before calling `the_post_thumbnail()` to avoid errors.
Recommended Links: