如何获取在主题中设置的所有可能缩略图大小的列表

时间:2011-11-11 作者:m-torin

我可以在插件中使用什么函数来获取子主题中定义的每个图像大小(最好是在数组中)的维度?

只是为了澄清,我不是在问如何创建新的图像大小。

6 个回复
最合适的回答,由SO网友:m-torin 整理而成

Found it here. 答案是:

global $_wp_additional_image_sizes; 
print \'<pre>\'; 
print_r( $_wp_additional_image_sizes ); 
print \'</pre>\';

SO网友:rjb

WordPress core doesn\'t have a native method for getting intermediate image sizes (i.e. width and height), but the following helper function will get all registered image sizes along with their dimensions:

/**
 * 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;
}

Which will output results similar to:

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] => 
        )

)
SO网友:sqren

如果只需要所有图像大小的名称,可以使用get_intermediate_image_sizes:

<pre>
<?php print_r(get_intermediate_image_sizes()); ?>
</pre>

SO网友:wp-mario.ru

由于WP 5.3,使用此功能就足够了:

wp_get_registered_image_subsizes();

SO网友:Shoaib Iqbal

如果另一个答案不起作用,请使用此代码,这样它将在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>\';
}

SO网友:Carlos G

由于WP 4.7,您可以使用wp_get_additional_image_sizes() 注册其他图像大小(不包括WP添加的默认图像大小)。

由于WP 5.3,您可以使用wp_get_registered_image_subsizes() 以注册所有图像大小。

结束

相关推荐

fetch images and videos

是否可以获取特定网站的视频和图像并将其发布?我的意思是我正在考虑写一个函数,在这里我只写网站名称,然后在网站url的帮助下,所有或最新的图片和视频都会发布到我的帖子上。这可能吗?