filters - How can I add image sizes for a specific custom post type?

What I'm trying to achieve is to define a specific image size (a portrait thumbnail) for a specific post type (book

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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

相关推荐

  • filters - How can I add image sizes for a specific custom post type?

    What I'm trying to achieve is to define a specific image size (a portrait thumbnail) for a specific post type (book

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信