This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
post_thumbnail_size filter
Filters the post thumbnail size.
apply_filters('post_thumbnail_size', string|int[] $size, int $post_id)
Description
This is a filter hook that filters the post thumbnail size. It consists of two parameters: $size and Post Id.
It is used in get_the_post_thumbnail() to get the post thumbnail.
Parameters
- $size: (string|int[]) Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
- $post_id: (int) The post ID.
Live Examples
Basic Usage
To run the hook, copy the example below.
$size = apply_filters('post_thumbnail_size', $size, $post_id); if (!empty($size)) { // everything has led up to this point... }
Adding a Hook Callback
The following example is for adding a hook callback.
// define the post_thumbnail_size callback function weplugins_filter_post_thumbnail_size($size, $post_id) { // make filter magic happen here... return $size; } // add the filter add_filter('post_thumbnail_size', 'weplugins_filter_post_thumbnail_size', 10, 2);
Customizing Thumbnail Size
In this example, we change the thumbnail size to ‘medium’ for a specific post ID.
function weplugins_custom_post_thumbnail_size($size, $post_id) { if ($post_id == 42) { return 'medium'; } return $size; } add_filter('post_thumbnail_size', 'weplugins_custom_post_thumbnail_size', 10, 2);
Contact Us
If you need customization, contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.