我正在尝试创建一个应该使用特色图像作为背景的帖子,如果没有选择图像,它应该使用背景色。
我在这里和谷歌上都搜索过,但我只能找到这个代码。
<?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>
不用说,它不太管用。
这样试试,
<?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>