将WordPress图像大小显示为用于页面生成器的数组

时间:2017-10-04 作者:ashraf

我使用此函数返回所有可用的图像大小作为数组。

function king_image_size() {
  $image_sizes = get_intermediate_image_sizes();
    foreach ($image_sizes as $tax) {
     $image_sizes[$tax] = $tax;
    }
   return $image_sizes
 }
但这与返回数组重复。输出

array(18) { [0]=> string(9) "thumbnail" [1]=> string(6) "medium" [2]=> string(12) "medium_large" [3]=> string(5) "large" [4]=> string(9) "dhaka-por" [5]=> string(11) "dhaka-por-h" [6]=> string(15) "dhaka-landscape" [7]=> string(7) "dhaka-h" [8]=> string(8) "dhaka-sq" ["thumbnail"]=> string(9) "thumbnail" ["medium"]=> string(6) "medium" ["medium_large"]=> string(12) "medium_large" ["large"]=> string(5) "large" ["dhaka-por"]=> string(9) "dhaka-por" ["dhaka-por-h"]=> string(11) "dhaka-por-h" ["dhaka-landscape"]=> string(15) "dhaka-landscape" ["dhaka-h"]=> string(7) "dhaka-h" ["dhaka-sq"]=> string(8) "dhaka-sq" }
我发现另一种很常见的方式,

function king_image_size() {
  $image_sizes = get_intermediate_image_sizes();
  return $image_sizes;
}
然后我得到0,1,2,3。。。在页面生成器中输出。我需要像这样工作的功能

function king_image_size(){
  return array(
      \'medium\' => \'medium\' ,
  );  
}

1 个回复
最合适的回答,由SO网友:Future is black 整理而成

请使用array_combine 像这样

function king_image_size() {
  $image_sizes = get_intermediate_image_sizes(); 
  return array_combine($image_sizes, $image_sizes);
}

结束

相关推荐