我正在定制一个 WordPress 主题,并面临一些特色图像的问题。
如果帖子或页面上有特色图片 - 并且 - 该特色图片为 960x250,则特色图片显示为该帖子或页面上的标题。
如果特色图片不是 960x250 则该特色图片应显示在主要内容区域中,水平填充空间(就像现在在帖子中一样)当且仅当特色图片为 960x250 时,它仅显示为标题,不在内容中
我正在定制一个 WordPress 主题,并面临一些特色图像的问题。
如果帖子或页面上有特色图片 - 并且 - 该特色图片为 960x250,则特色图片显示为该帖子或页面上的标题。
如果特色图片不是 960x250 则该特色图片应显示在主要内容区域中,水平填充空间(就像现在在帖子中一样)当且仅当特色图片为 960x250 时,它仅显示为标题,不在内容中
正确的写作方式是
if ( has_post_thumbnail() ) {
$headerImg = get_the_post_thumbnail($post->ID, array(960,250) );
}
has_post_thumbnail()
只接受可选的 id
已编辑
$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
}