Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将特征图像设置为div中的背景_Php_Css_Wordpress - Fatal编程技术网

Php 将特征图像设置为div中的背景

Php 将特征图像设置为div中的背景,php,css,wordpress,Php,Css,Wordpress,在浏览了互联网之后,我发现了一种方法,可以将Wordpress的特色图片设置为div中的背景图片。现在的问题是,我只能将该div的背景图片的样式设置为行。我希望能够在css文件中设置样式,但不确定如何设置 这是我的密码: <?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?> <div style="bac

在浏览了互联网之后,我发现了一种方法,可以将Wordpress的特色图片设置为div中的背景图片。现在的问题是,我只能将该div的背景图片的样式设置为行。我希望能够在css文件中设置样式,但不确定如何设置

这是我的密码:

<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<div style="background: url(<?php echo $src[0]; ?> ) no-repeat center; width:100%; height: 300px; background-size:cover;"></div>


CSS文件是静态的,但是WordPress的特色图片和附件是动态的。处理此问题的最佳方法是设置
背景图像
内联,并在CSS文件中设置其余的背景属性

PHP

<div class="featured-image" style="background-image: url('<?php echo esc_url( $src[0] ); ?>')"></div>

将此代码与内联css一起使用
echo wp\u get\u attachment\u url(get\u post\u thumbnail\u id())

就是这样!我以前试过这样做,但没有成功。
.featured-image {
    background: no-repeat center;
    width: 100%;
    height: 300px;
    background-size: cover;
}