【问题标题】: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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-28
      • 2012-10-24
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      相关资源
      最近更新 更多