2

我正在寻找一种方法来避免在 CPT 中上传新图像时创建图像缩略图。

所以在我的functions.php中我有:

if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 56, 56, true ); // Normal post thumbnails
add_image_size( 'tinyfeatured', 50, 50, true ); // Tiny Featured thumbnail
add_image_size( 'blogonecol', 530, '', true ); // Blog One Column thumbnail
add_image_size( 'post', 530, '', true ); // Portfolio Large thumbnail
add_image_size( 'postnc', 700, '', false ); // Portfolio Large thumbnail
add_image_size( 'carousel', 560, 341, true ); // Portfolio Large thumbnail
add_image_size( 'magazine-full', '', 1131, true); // Portfolio Large thumbnail
}

为了避免为我正在使用的 CPT 创建所有这些图像尺寸,我这样做了:

if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
if( !is_singular('issues') ) {
set_post_thumbnail_size( 56, 56, true ); // Normal post thumbnails
add_image_size( 'tinyfeatured', 50, 50, true ); // Tiny Featured thumbnail
add_image_size( 'blogonecol', 530, '', true ); // Blog One Column thumbnail
add_image_size( 'post', 530, '', true ); // Portfolio Large thumbnail
add_image_size( 'postnc', 700, '', false ); // Portfolio Large thumbnail
add_image_size( 'carousel', 560, 341, true ); // Portfolio Large thumbnail
}
add_image_size( 'magazine-full', '', 1131, true); // Portfolio Large thumbnail
}

但是,当我在 CPT“问题”中上传新图像时,会创建上述所有图像大小。

还有其他方法可以避免这种情况吗?

4

1 回答 1

0

尝试unset( $sizes['tinyfeatured']);在条件内使用,因此当它是 CPT“问题”时,您取消设置大小,但当它相反时设置大小。像这样:

if( is_singular('issues') ) {
  function sgr_filter_image_sizes( $sizes) {
    
    unset( $sizes['tinyfeatured']);
    unset( $sizes['blogonecol']);
    unset( $sizes['post']);
    unset( $sizes['postnc']);
    unset( $sizes['carousel']);

    return $sizes;
  }
    add_filter('intermediate_image_sizes_advanced', 'sgr_filter_image_sizes');
}

更多信息并引用自删除 WordPress 默认媒体图像大小

编辑 试试这个尺寸

function add_new_thumbnails() {
  if ( function_exists( 'add_theme_support' ) ) {
  add_theme_support( 'post-thumbnails' );

  set_post_thumbnail_size( 56, 56, true ); // Normal post thumbnails
  add_image_size( 'tinyfeatured', 50, 50, true ); // Tiny Featured thumbnail
  add_image_size( 'blogonecol', 530, '', true ); // Blog One Column thumbnail
  add_image_size( 'post', 530, '', true ); // Portfolio Large thumbnail
  add_image_size( 'postnc', 700, '', false ); // Portfolio Large thumbnail
  add_image_size( 'carousel', 560, 341, true ); // Portfolio Large thumbnail
  add_image_size( 'magazine-full', '', 1131, true); // Portfolio Large thumbnail
  }
 }
 add_action('after_setup_theme', 'add_new_thumbnails',10); //10 is default

将其与顶部代码一起使用,但添加过滤器优先级12

add_filter('intermediate_image_sizes_advanced', 'sgr_filter_image_sizes',12);

试试这个链接 我的片段

把它放在你的functions.php中,而不是你添加的代码

新方法

因此,一种新的尝试方法是:

function add_new_thumbnails() {
 add_theme_support( 'post-thumbnails' );

 set_post_thumbnail_size( 56, 56, true ); // Normal post thumbnails
 add_image_size( 'tinyfeatured', 50, 50, true ); // Tiny Featured thumbnail
 add_image_size( 'blogonecol', 530, '', true ); // Blog One Column thumbnail
 add_image_size( 'post', 530, '', true ); // Portfolio Large thumbnail
 add_image_size( 'postnc', 700, '', false ); // Portfolio Large thumbnail
 add_image_size( 'carousel', 560, 341, true ); // Portfolio Large thumbnail
 add_image_size( 'magazine-full', '', 1131, true); // Portfolio Large thumbnail
}

add_action('init', 'add_new_thumbnails',10); //10 is default

if ( is_singular('issues') ) {
function remove_thumbnails() {
    add_image_size( 'tinyfeatured', 0, 0 ); // Tiny Featured thumbnail
    add_image_size( 'blogonecol', 0, 0); // Blog One Column thumbnail
    add_image_size( 'post', 0,0 ); // Portfolio Large thumbnail
    add_image_size( 'postnc',0,0 ); // Portfolio Large thumbnail
    add_image_size( 'carousel',0,0 ); // Portfolio Large thumbnail
    add_image_size( 'magazine-full',0,0); // Portfolio Large thumbnail
 }
add_action('init', 'remove_thumbnails',12); // 12 executes after 10

}
于 2013-03-18T13:19:00.543 回答