自定义WordPress特色缩略图尺寸的4种方法

本人一直很困惑,WordPress特色缩略图featured image到底是如何设置的?它又到底是如何调用的?阅读官方文件才能得出最真实的答案。

特色图像(有时也称为帖子缩略图)是代表单个帖子、页面或自定义帖子类型的图像。当您创建主题时,您可以以多种不同的方式输出特色图像,例如,在归档页面上、在标题中或在帖子上方。

要启用对特色图像的支持,主题必须声明支持专题图像功能,专题图像界面才会出现在编辑屏幕上。通过将以下内容放入主题的functions.php文件中来声明支持:

add_theme_support( 'post-thumbnails' );

开启特色图片后,默认情况下,“特色图像”选择框显示在“编辑文章”和“编辑页面”屏幕的边栏中。下面就来讲述4种设计特色缩略图的方法:

1,使用默认尺寸

在后台-设置-媒体,可以看到有三个尺寸;如果没有做任何修改,系统会默认调用Thumbnail size 缩略图尺寸为默认的Feature image特色图片,调用代码为

the_post_thumbnail();

此尺寸也可以直接修改后台-设置-媒体-Thumbnail size 缩略图 来达成自己的目标尺寸。

2,使用参数设置

如果你不想使用Thumbnail 或者它被占用,你可以使用参数来调用其它图片尺寸设置为Feature image特色图片,调用代码如下:

the_post_thumbnail(); // 无参数,默认调用 ->; Thumbnail
the_post_thumbnail( 'thumbnail' ); // 参数Thumbnail (默认尺寸 150px x 150px max)
the_post_thumbnail( 'medium' ); // 参数Medium (默认尺寸 300px x 300px max)
the_post_thumbnail( 'medium_large' ); // 参数Medium-large (默认尺寸 768px x no height limit max)
the_post_thumbnail( 'large' ); // 参数Large (默认尺寸 1024px x 1024px max)
the_post_thumbnail( 'full' ); // 参数full (上传原图片尺寸,无修改)
the_post_thumbnail( array( 100, 100 ) ); // 自定义尺寸 (height, width)

其中默认尺寸仍然可以从后台-设置-媒体下的选项中更改。

3,专用设置特色图片尺寸代码

WordPress内置了专门修改Feature image特色图片的代码,您可以使用set_post_thumbnail_size()设置默认的精选图片大小,也可按比例调整图像大小(即,不扭曲图片):

裁剪模式:

set_post_thumbnail_size( 800, 250, true );

如果不加参数true, 有可能新尺寸会扭曲图片。此代码可以直接修改默认的特色图片尺寸,而不用到后台-设置-媒体下手动修改了。但此代码必须放置在functions.php文件中方可生效。

4,创建自定义名称,自定义尺寸的特色图片

除了上述三种方法设置默认的Feature Image特色图片外,还可以通过自定义方法增加更多的Feature image特色图片尺寸。也就是说特色图片它并不是唯一的,不同的帖子格式可以设置不同尺寸特色图片。

if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 150, 150, true ); // 修改默认特色图片尺寸 (裁切模式)
 
    // 附加图像尺寸
    add_image_size( 'category-thumb', 300, 9999 ); // 300像素宽x无限高度
 }

在functions.php加入以上代码后,你的主题便支持了新的特色图片尺寸,使用以下代码进行调用:

php the_post_thumbnail( 'category-thumb' );

另外,如果要批量调用一篇文章中所有图片的此格式,可以使用以下代码来循环显示:

$argsThumb = array(
	'order'          => 'ASC',
	'post_type'      => 'attachment',
	'post_parent'    => $post->ID,
	'post_mime_type' => 'image',
	'post_status'    => null
);
$attachments = get_posts($argsThumb);

if ($attachments) {
	$i = 1;
	foreach ($attachments as $attachment) {
		$i++;
		$image_attributes = wp_get_attachment_image($attachment->ID, 'small-thumbnail');

		echo $image_attributes;
		
		if ( $i > 3) {
			break;
		}
	}
}

这里随意提到一个技巧是,使用Crop Featured Image插件,可以有选择性的为不同文章选择所有已经注册的特色图片。
随便送一下,当没有设置特色图片时,自动调用第一张图片作为特色图片

//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $main=wp_get_attachment_url($num);
        $template=get_template_directory();
        $the_title=get_the_title();
    print "<img class="frame" src="$main" alt="$the_title" />";
  endif;
}

Previous/Next

Say Something!

Leave a Reply