【问题标题】:Wordpress: featured image else background colorWordpress:特色图片其他背景颜色
【发布时间】:2015-04-11 05:10:12
【问题描述】:
我正在尝试创建应使用特色图片作为背景的帖子,如果未选择任何图片,则应使用背景颜色。
我在这里和谷歌都搜索过,但我只能找到这段代码。
<?php
if (has_post_thumbnail( $post->ID ) ):
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
else:
$image_attributes = array( '#fa5252; opacity:1!important' );
endif;
?>
<div style="background: url(<?php echo $image_attributes[0]; ?>)"></div>
不用说,它并不完全有效。
【问题讨论】:
标签:
wordpress
image
featured
【解决方案1】:
这样试试,
<?php
$background = '';
if (has_post_thumbnail( $post->ID ) ):
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
$background = 'url('.$image_attributes[0].')';
else:
$image_attributes = array( ' ' );
$background = '#fa5252; opacity:1!important';
endif;
?>
<div style="background: <?php echo $background ; ?>">Content</div>