WordPress image thumbnail management and display images by ID

Last update: 22 Квітня, 2023

Category: WordPress

Code to the functions.php

function dco_remove_default_image_sizes( $sizes ) {
    return array_diff( $sizes, array(
        //'thumbnail',
        'medium',
        'medium_large',
        'large',
        '1536x1536',
        '2048x2048',
    ) );
}
add_filter('intermediate_image_sizes', 'dco_remove_default_image_sizes');

Example of display custom field (image) by image ID

<?php
    if (get_field('gallery')) {
        while (has_sub_field('gallery')) {
            if ((get_sub_field('image')) != '') {
                // echo $id = get_sub_field('image');
                ?>
                    <a data-fancybox="gallery" data-src="<?php echo wp_get_attachment_image_src(get_sub_field('image'), 'full')[0];  ?>" class="gallery_sub_item__square">
                        <div class="gallery_sub_item__content">
                            <img class="" src="<?php echo wp_get_attachment_image_src(get_sub_field('image'), 'thumbnail')[0];  ?>" />
                            <?php echo $id = get_sub_field('description'); ?>
                        </div>
                    </a>
                <?php
            }
        }
    }
?>

As you see we use:

$id = get_sub_field('image');
// and
echo wp_get_attachment_image_src($id, 'full')[0];
// and
echo wp_get_attachment_image_src($id, 'thumbnail')[0];

You can change custom size of the Thumbnail on the page /wp-admin/options-media.php.