wp_get_registered_image_subsizes

函数
wp_get_registered_image_subsizes ( No parameters )
返回值
  • (array[]) Associative array of arrays of image sub-size information, keyed by image size name.
定义位置
相关方法
wp_create_image_subsizeswp_get_missing_image_subsizesget_intermediate_image_sizeswp_update_image_subsizesget_registered_meta_keys
引入
5.3.0
弃用
-

wp_get_registered_image_subsizes: 这个函数用来检索在WordPress注册的图片尺寸的信息。它返回一个尺寸对象的数组,每个对象都包含关于尺寸的信息,例如它的名称、尺寸和裁剪设置。

返回所有当前注册的图像子尺寸的规范化列表。

function wp_get_registered_image_subsizes() {
	$additional_sizes = wp_get_additional_image_sizes();
	$all_sizes        = array();

	foreach ( get_intermediate_image_sizes() as $size_name ) {
		$size_data = array(
			'width'  => 0,
			'height' => 0,
			'crop'   => false,
		);

		if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
			// For sizes added by plugins and themes.
			$size_data['width'] = (int) $additional_sizes[ $size_name ]['width'];
		} else {
			// For default sizes set in options.
			$size_data['width'] = (int) get_option( "{$size_name}_size_w" );
		}

		if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
			$size_data['height'] = (int) $additional_sizes[ $size_name ]['height'];
		} else {
			$size_data['height'] = (int) get_option( "{$size_name}_size_h" );
		}

		if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
			// This size isn't set.
			continue;
		}

		if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
			$size_data['crop'] = $additional_sizes[ $size_name ]['crop'];
		} else {
			$size_data['crop'] = get_option( "{$size_name}_crop" );
		}

		if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
			$size_data['crop'] = (bool) $size_data['crop'];
		}

		$all_sizes[ $size_name ] = $size_data;
	}

	return $all_sizes;
}

常见问题

FAQs
查看更多 >