如何在WordPress中使用图像尺寸960 * 250时显示要素图像

时间:2013-03-07 12:15:06

标签: wordpress

我正在自定义WordPress主题并面临特色图片的一些问题。

如果帖子或页面上有特色图片 - 并且 - 特色图片为960x250那么特色图片就会显示为该帖子或页面上的标题。

如果特色图片不是960x250那么该特色图片应该显示在主要内容区域中,水平填充空间(就像现在的帖子一样) 如果且仅当特色图像为960x250时,它才会显示为标题,而不是内容

1 个答案:

答案 0 :(得分:1)

正确的写作方式

if ( has_post_thumbnail() ) {
  $headerImg = get_the_post_thumbnail($post->ID, array(960,250) );
}

has_post_thumbnail()只接受id

<强> EDITED

$headerImg = wp_get_attachment_image_src($post->ID, array(960,250));

if (has_post_thumbnail() && $headerImg[1] == 960 && $headerImg[2] == 250) {
 // if it is output is 960x250 in size
 $headerImg = the_post_thumbnail(array(960,250));
} else {
 // if it isn't then show the medium or some other size image
 $contentImg = the_post_thumbnail('medium');
}

进一步编辑

$headerImg = get_the_post_thumbnail($post->ID, array(960,250) );
if (has_post_thumbnail() && !empty($headerImg) ){
 #This checks to see if the post has the thumbnail and the image is 960x250.
 #Therefore you can now add it to the header.
 $headerImg = get_the_post_thumbnail($post->ID, array(960,250) );
} elseif (has_post_thumbnail() && empty($headerImg) {
   #This checks to see if thumbnail exists and works if 960x250 is empty
} else {
 #Place code here if the featured image doesnt exist
}