Answers:
如果只需要所有图像尺寸的名称,则可以使用get_intermediate_image_sizes:
<pre>
<?php print_r(get_intermediate_image_sizes()); ?>
</pre>
WordPress核心没有用于获取中间图像大小(即宽度和高度)的本机方法,但是以下辅助函数将获取所有已注册图像大小及其尺寸:
/**
* Get all the registered image sizes along with their dimensions
*
* @global array $_wp_additional_image_sizes
*
* @link http://core.trac.wordpress.org/ticket/18947 Reference ticket
*
* @return array $image_sizes The image sizes
*/
function _get_all_image_sizes() {
global $_wp_additional_image_sizes;
$default_image_sizes = get_intermediate_image_sizes();
foreach ( $default_image_sizes as $size ) {
$image_sizes[ $size ][ 'width' ] = intval( get_option( "{$size}_size_w" ) );
$image_sizes[ $size ][ 'height' ] = intval( get_option( "{$size}_size_h" ) );
$image_sizes[ $size ][ 'crop' ] = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false;
}
if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
$image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
}
return $image_sizes;
}
它将输出类似于以下内容的结果:
Array
(
[thumbnail] => Array
(
[width] => 150
[height] => 150
[crop] => 1
)
[medium] => Array
(
[width] => 300
[height] => 300
[crop] =>
)
[medium_large] => Array
(
[width] => 768
[height] => 0
[crop] =>
)
[large] => Array
(
[width] => 1024
[height] => 1024
[crop] =>
)
)
$default_image_sizes = get_intermediate_image_sizes();
就有一个medium_large
尺寸。
如果其他答案无效,请使用此代码,因此它将在WordPress初始化后运行。
add_action('init', 'get_all_image_sizes');
function get_all_image_sizes(){
global $_wp_additional_image_sizes;
print '<pre>';
print_r( $_wp_additional_image_sizes );
print '</pre>';
}