User_FTW
User_FTW

Reputation: 524

List all image sizes including custom (WordPress)

This script mostly works:

$_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 />';
}

The default WordPress image sizes (thumbnail, medium, large etc) show their correct dimensions.

But the problem is that custom image sizes always show their width and height as 0.

This is what is returned:

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

What am I doing wrong?

PS: I've registered additional images sizes like this:

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

Upvotes: 3

Views: 10538

Answers (3)

wp-mario.ru
wp-mario.ru

Reputation: 914

Since WP 5.3 it is enough to use this function:

wp_get_registered_image_subsizes();

Upvotes: 12

Santu Roy
Santu Roy

Reputation: 86

Try this code

this function will return all registered & default sizes with height, width

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

Like this

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

Upvotes: 6

Sagar Bahadur Tamang
Sagar Bahadur Tamang

Reputation: 2709

Try the following code.

//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']})";
}

Upvotes: 0

Related Questions