当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

関数リファレンス/add image size

提供: WordPress Codex 日本語版
< 関数リファレンス
2013年7月14日 (日) 17:44時点におけるDjkaz (トーク | 投稿記録)による版 (英語版をコピペ。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Description

Registers a new image size. This means that WordPress will create a copy of the Featured Image (formerly known as post thumbnail) with the specified dimensions when you upload a new image.

Note: To enable featured images, or post thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.

Usage

%%%<?php add_image_size( $name, $width, $height, $crop ); ?>%%%

Parameters

$name
string) (必須) The new image size name.
初期値: なし
$width
int) (optional) The post thumbnail width in pixels.
初期値: 0
$height
int) (optional) The post thumbnail height in pixels.
初期値: 0
$crop
boolean) (optional) Crop the image or not. False - Soft proportional crop mode ; True - Hard crop mode.
初期値: false

Reserved Image Size Names

thumb, thumbnail, medium, large, post-thumbnail

The names "thumb" & "thumbnail" are just alias, so exactly the same.

For a detailed explanation and "why", read further inside the image_downsize() article.

However, if needed, you can always set the options yourself:

update_option('thumbnail_size_w', 160);
update_option('thumbnail_size_h', 160);
update_option('thumbnail_crop', 1);

Examples

A theme's functions.php file.

if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions   
}

if ( function_exists( 'add_image_size' ) ) { 
	add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
	add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
}

Crop Mode

Set the image size by resizing the image proportionally (that is, without distorting it):

add_image_size( 'homepage-thumb', 220, 180 ); // 220 pixels wide by 180 pixels tall, soft proportional crop mode

Set the image size by cropping the image (either from the sides, or from the top and bottom):

add_image_size( 'homepage-thumb', 220, 180, true ); // 220 pixels wide by 180 pixels tall, hard crop mode

Using the New Image Sizes

Within a theme's template files.

<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); } ?>

Notes

Using the 'false' setting will fail to produce a new image in the upload directory if one of the image dimensions of the uploaded image are equal to the new image size.

Change Log

Source File

add_image_size() is located in wp-includes/media.php.

Resources

Regenerate Thumbnails - This plugin regenerate the thumbnails for your image attachments. This is very handy if you've changed any of your thumbnail dimensions (via Settings -> Media) after previously uploading images or have changed to a theme with different featured post image dimensions.

AJAX thumbnail rebuild - This plugin allows you to re-build the post thumbnails. Useful if you use add_image_size() after already having uploaded post thumbnails. (This plugin is slow, but never has out of memory errors).

Simple Image Sizes - This plugin allows you create new sizes of thumbnails directly from the "Media" panel. It allows you to regenerate the thumbnails too. The plugin adds the thumbnails to your post sizes and you can add them directly on your posts. You can choose the sizes you wanted to regenerate and the posts types too.

Related

投稿サムネイル: has_post_thumbnail, the_post_thumbnail, get_post_thumbnail_id, get_the_post_thumbnail, add_image_size(), set_post_thumbnail_size()


関数リファレンステンプレートタグ目次もご覧ください。