Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

wp_get_attachment_image_sizes

Function Description:

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Retrieve all available image sizes for a specific attachment ID
$attachment_id = 123;
$image_sizes = wp_get_attachment_image_sizes($attachment_id);
if ($image_sizes) {
    foreach ($image_sizes as $size => $size_data) {
        echo "Size: $size, Width: {$size_data['width']}, Height: {$size_data['height']}\n";
    }
} else {
    echo "No image sizes found for attachment ID: $attachment_id\n";
}
// Example 2: Check if a specific image size exists for a given attachment ID
$attachment_id = 456;
$size_to_check = 'medium';
$image_sizes = wp_get_attachment_image_sizes($attachment_id);
if (array_key_exists($size_to_check, $image_sizes)) {
    echo "Size '$size_to_check' exists for attachment ID: $attachment_id\n";
} else {
    echo "Size '$size_to_check' does not exist for attachment ID: $attachment_id\n";
}
// Example 3: Get the URL of a specific image size for a particular attachment ID
$attachment_id = 789;
$size_to_get = 'thumbnail';
$image_sizes = wp_get_attachment_image_sizes($attachment_id);
if (array_key_exists($size_to_get, $image_sizes)) {
    $image_url = wp_get_attachment_image_url($attachment_id, $size_to_get);
    echo "URL of '$size_to_get' size for attachment ID $attachment_id: $image_url\n";
} else {
    echo "Size '$size_to_get' does not exist for attachment ID: $attachment_id\n";
}