列出所有图像尺寸,包括自定义(WordPress)

时间:2019-03-03 07:47:45

标签: php wordpress foreach

此脚本通常有效:

$_wp_additional_image_sizes[$name] = array($width, $height, $crop);
$image_sizes = get_intermediate_image_sizes();
foreach ($image_sizes as $size) {
    echo $size . ' ';
    echo $image_sizes[ $size ][ 'width' ] = intval( get_option( "{$size}_size_w" ) );
    echo ' x ';
    echo $image_sizes[ $size ][ 'height' ] = intval( get_option( "{$size}_size_h" ) )  . '<br />';
}

默认的WordPress图片大小(缩略图,中号,大号等)显示其正确尺寸。

但是问题在于自定义图像尺寸始终将其宽度和高度显示为0。

这是返回的内容:

thumbnail 150 x 150
medium 300 x 300
medium_large 768 x 0
large 1024 x 1024
post-thumbnail 0 x 0
Custom Blog Image 0 x 0
Additional Food Image 0 x 0

我在做什么错了?

PS:我已经注册了其他图像尺寸,如下所示:

add_image_size( 'Custom Blog Image', 1600, 800, $crop = true );
add_image_size( 'Additional Food Image', 800, 600, $crop = true );

3 个答案:

答案 0 :(得分:2)

自WP 5.3起,使用此功能就足够了:

wp_get_registered_image_subsizes();

答案 1 :(得分:1)

尝试使用此代码

此函数将返回所有已注册的默认尺寸以及高度,宽度

function your_thumbnail_sizes() {
        global $_wp_additional_image_sizes;
        $sizes = array();
        $rSizes = array();
        foreach (get_intermediate_image_sizes() as $s) {
            $sizes[$s] = array(0, 0);
            if (in_array($s, array('thumbnail', 'medium', 'medium_large', 'large'))) {
                $sizes[$s][0] = get_option($s . '_size_w');
                $sizes[$s][1] = get_option($s . '_size_h');
            }else {
                if (isset($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes[$s]))
                    $sizes[$s] = array($_wp_additional_image_sizes[$s]['width'], $_wp_additional_image_sizes[$s]['height'],);
            }
        }
        foreach ($sizes as $size => $atts) {
            $rSizes[$size] = $size . ' ' . implode('x', $atts);
        }
        return $rSizes;
    }

Array
(
    [thumbnail] => thumbnail 150x150
    [medium] => medium 300x300
    [medium_large] => medium_large 768x0
    [large] => large 1024x1024
    [custom-size] => custom-size 220x180
)

答案 2 :(得分:0)

尝试以下代码。

//get the image size.
$image_sizes = wp_get_additional_image_sizes();
foreach ( $image_sizes as $key => $image_size ) {
    echo "{$key} ({$image_size['width']} x {$image_size['height']})";
}