Uploaded Images
WordPress creates resized copies of each uploaded image. When you show uploaded images in a list or a slider, add an image size of the right dimensions so visitors don't download the full-size image.
Adding an image size
Add add_image_size() in include/init.php. The template already has a commented-out line for it.
// include/init.php
add_action('init', function() {
// ...
// アイキャッチ画像のサイズ追加
// Adding image size for post thumbnail
add_image_size('news-thumb', 600, 400, true);
});
add_image_size(
'news-thumb', // Name of the image size
600, // Width
400, // Height
true // true: crop to exactly this size from the center
// false (default): scale down, keeping the aspect ratio
);
The new size is created only for images uploaded after you add it. To create it for existing images too, regenerate the thumbnails with WP-CLI (wp media regenerate).
In templates
Pass the size name to the_post_thumbnail(). The second argument adds attributes to the <img> tag.
<?php if (has_post_thumbnail()): ?>
<?php the_post_thumbnail('news-thumb', ['class' => 'c-post-list__thumb__image']); ?>
<?php else: ?>
<img src="<?php the_asset_url('data/img/common/noimage.jpg'); ?>" class="c-post-list__thumb__image" alt="">
<?php endif; ?>
For an image in a custom field, use wp_get_attachment_image() with the attachment ID.
<?php echo wp_get_attachment_image($image['ID'], 'medium_large', false, ['class' => 'p-case-detail__gallery__image']); ?>