简体   繁体   中英

Custom image size for a custom post type in Wordpress

I'm trying to add custom image size to posts of a custom post type articles by following function in functions.php :

function thumb_size($id)
{
    if(get_post_type() == "articles")
    {
        add_image_size('articles-thumb', 113, 72, true);
    }
}
add_action ( 'publish_post', 'thumb_size' );

and trying to show it by the following code:

the_post_thumbnail('articles-thumb');

But what I see as output, doesn't have the same size as I declared, what's the problem?

I'm facing the same issue, generating a lot of image variations for any upload kind isn't a good idea. I have a custom post type 'collection' which generates 2 different sizes that are exclusive to collection, why would I generate those sizes every upload? I could not find a solution for this issue.

Just use the single line add_image_size('articles-thumb', 113, 72, true); in your functions.php file. Remove the reset of your function and add action code.

Then display it with the line you have in your question. the_post_thumbnail('articles-thumb');

You may also need to regenerate your thumbnails. This plugin works great: http://wordpress.org/extend/plugins/regenerate-thumbnails/ If your images are uploaded prior to that image size being set there won't be a thumbnail that size. Use the plugin to regenerate a thumbnail for a single image or all images you have uploaded. Each one regenerated will now have a thumbnail of all custom sizes.

After uploading any image to WordPress by default it generate 3 default size of the image Thumbnail Medium Size Large Size Stop any user to use this images sizes on your current theme use this following code in your functions.php inside theme folder Remove these using WordPress action intermediate_image_sizes_advanced then unset the thumbnail, medium and large sizes, it means there only left full image size.

function w3learn_filter_image_sizes( $sizes) {

    unset( $sizes['thumbnail']);
    unset( $sizes['medium']);
    unset( $sizes['large']);

    return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'w3learn_filter_image_sizes');`

Details on my Removing default media image sizes in wordpress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM