我正在寻找一种方法来避免在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“问题”中上传新图像时,会创建上述所有图像尺寸。
还有另一种方法可以避免这种情况吗?
答案 0 :(得分: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');
}
更多信息并引自Remove WordPress default media image sizes
<强> EDITED 强> 试试这个尺寸
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);
试试此链接 my snippet
将它放在你的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
}