无法使add_image_size工作

时间:2016-10-13 22:04:31

标签: wordpress

我正在创建一个WordPress主题,并尝试为用户上传到高级自定义字段图像字段的图像创建各种不同大小的图像。但是,当我使用下面的代码时,请使用Regenerate Thumbnails插件创建新的图像大小,但它似乎没有任何影响。

我感觉那里有一些非常基本的东西,因为我读过的所有内容都应该有效。下面的代码是实际功能的缩减版本,在我使用的那个图片中可以使用更多的图像尺寸。

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

  // Images used on blog index & category pages for the first item and at the top of the blog post page itself
  add_image_size( "main-image", 700, 680, true );
  add_image_size( "main-image-350", 350, 450, true ); // For use on screens 350 pixels & below
  add_image_size( "main-image-539", 539, 573, true ); // For use on screens 539 pixels & below
  add_image_size( "main-image-659", 659, 570, true ); // For use on screens 659 pixels & below
  add_image_size( "main-image-850", 850, 650, true ); // For use on screens 850 pixels & below
  add_image_size( "main-image-1000", 1000, 650, true ); // For use on screens 1000 pixels & below
  add_image_size( "main-image-1200", 1200, 680, true ); // For use on screens 1200 pixels & below
  add_image_size( "main-image-1400", 1400, 680, true ); // For use on screens 1200 pixels & above
}
add_action( 'init', 'test_add_image_sizes' );

上面的代码放在我的主题functions.php文件中。

2 个答案:

答案 0 :(得分:0)

这就是我的意思:

// Enable post thumbnails
add_theme_support('post-thumbnails');

// Custom image sizes
if ( function_exists( 'add_image_size' ) ) { 
	add_image_size( 'carousel-mobile', 640, 400, true );
	add_image_size( 'carousel-tablet', 980, 602, true );
	add_image_size( 'carousel-desk', 1400, 580, true );
	add_image_size( 'feed-thumb', 300, 185, true );
	add_image_size( 'article-featured', 640, 300, true );
	add_image_size( 'list-thumb', 382, 204, true );
}

使用ACF确保将图像设置为数组,然后像这样编码

<?php $image = get_field('image'); ?>

<img src="<?php echo $image['sizes']['list-thumb']; ?>" alt="">  

答案 1 :(得分:0)

您可以使用after_theme_setup挂钩代替init。用户为先前上传的图像尺寸修复重新生成缩略图插件。

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

  // Images used on blog index & category pages for the first item and at the top of the blog post page itself
  add_image_size( "main-image", 700, 680, true );
  add_image_size( "main-image-350", 350, 450, true ); // For use on screens 350 pixels & below
  add_image_size( "main-image-539", 539, 573, true ); // For use on screens 539 pixels & below
  add_image_size( "main-image-659", 659, 570, true ); // For use on screens 659 pixels & below
  add_image_size( "main-image-850", 850, 650, true ); // For use on screens 850 pixels & below
  add_image_size( "main-image-1000", 1000, 650, true ); // For use on screens 1000 pixels & below
  add_image_size( "main-image-1200", 1200, 680, true ); // For use on screens 1200 pixels & below
  add_image_size( "main-image-1400", 1400, 680, true ); // For use on screens 1200 pixels & above
}
add_action( 'after_theme_setup', 'test_add_image_sizes' );