What I'm trying to achieve is to define a specific image size (a portrait thumbnail) for a specific post type (books), so that only this post type has this thumbnail size generated and I'm not wasting disk space by creating this thumbnail format for all the other post types present on the site.
I am trying to follow the logic of this answer to a similar question and this is the code I came up so far:
// Delete all intermediate image sizes
add_filter( 'intermediate_image_sizes', '__return_empty_array', 99 );
// Add custom image sizes depending on post type
function add_custom_post_types_image_sizes( $metadata, $attachment_id ) {
$post_parent_id = wp_get_post_parent_id( $attachment_id );
// For books
if ( 'book' === get_post_type( $post_parent_id ) ) {
// Add portrait thumbnail
add_image_size( 'portrait_thumbnail', '150', '215', true );
// This doesn't work :
$file = get_attached_file( $attachment_id );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
}
return $metadata;
}
add_filter( 'wp_generate_attachment_metadata', 'add_custom_post_types_image_sizes', 10, 2 );
What I'm trying to achieve is to define a specific image size (a portrait thumbnail) for a specific post type (books), so that only this post type has this thumbnail size generated and I'm not wasting disk space by creating this thumbnail format for all the other post types present on the site.
I am trying to follow the logic of this answer to a similar question and this is the code I came up so far:
// Delete all intermediate image sizes
add_filter( 'intermediate_image_sizes', '__return_empty_array', 99 );
// Add custom image sizes depending on post type
function add_custom_post_types_image_sizes( $metadata, $attachment_id ) {
$post_parent_id = wp_get_post_parent_id( $attachment_id );
// For books
if ( 'book' === get_post_type( $post_parent_id ) ) {
// Add portrait thumbnail
add_image_size( 'portrait_thumbnail', '150', '215', true );
// This doesn't work :
$file = get_attached_file( $attachment_id );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
}
return $metadata;
}
add_filter( 'wp_generate_attachment_metadata', 'add_custom_post_types_image_sizes', 10, 2 );
Share
Improve this question
asked Aug 1, 2019 at 14:37
mike23mike23
6,0397 gold badges48 silver badges71 bronze badges
2
- All images uploaded go into the media library and can be used anywhere else in WordPress. This means that all images need to be available in all sizes. In WordPress it doesn't make sense to say that images are "for" a specific post type. It might make sense for business reasons, but there's nothing in WordPress that technically enforces this. If your hosting is so stingy that an extra 150x215 image size is causing you problems, find a new host. – Jacob Peattie Commented Aug 2, 2019 at 1:01
- The only real solution to this problem other than that is to add a server for on-the-fly image optimisation, but that's beyond the scope of a single answer. However there are plugins available, like Jetpack's Photon or Smush CDN, that support this. – Jacob Peattie Commented Aug 2, 2019 at 1:01
1 Answer
Reset to default 0You can try this (not tested):
// Add image sizes for book outside
add_image_size( 'book_portrait_thumbnail', '150', '215', true );
add_image_size( 'book_portrait_square', '300', '300', true );
// Delete all intermediate image sizes
add_filter( 'intermediate_image_sizes', 'custom_image_sizes', 99 );
function custom_image_sizes( $image_sizes ) {
//In case of book return book sizes array
if( isset($_REQUEST['post_id']) && 'books' === get_post_type( $_REQUEST['post_id'] ) ){
return array( 'book_portrait_thumbnail', 'book_portrait_square' );
}
// Otherwise the other ones
return $image_sizes;
}
Of course, this is for science sake. Like others suggested I would rather find a different hosting provider, this is not relevant for the current state of tech offer.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745275473a4620013.html
评论列表(0条)