我正在自定义WordPress主题并面临特色图片的一些问题。
如果帖子或页面上有特色图片 - 并且 - 特色图片为960x250那么特色图片就会显示为该帖子或页面上的标题。
如果特色图片不是960x250那么该特色图片应该显示在主要内容区域中,水平填充空间(就像现在的帖子一样) 如果且仅当特色图像为960x250时,它才会显示为标题,而不是内容
答案 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
}