admin管理员组文章数量:1130349
I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.
if ( $post->post_parent )
$post_id = $post->post_parent;
else
$post_id = $post->ID;
if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
echo $thumbnail;
}
I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.
if ( $post->post_parent )
$post_id = $post->post_parent;
else
$post_id = $post->ID;
if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
echo $thumbnail;
}
Share
Improve this question
edited Nov 15, 2018 at 13:08
fuxia♦
107k39 gold badges255 silver badges461 bronze badges
asked Nov 15, 2018 at 6:20
William JeromeWilliam Jerome
1836 silver badges13 bronze badges
1
- 1 You can use get_the_post_thumbnail_url() for get image url and put into <div class="bg-img" style="background:PHP VARIABLE HERE"> – Pratik Patel Commented Nov 15, 2018 at 6:24
1 Answer
Reset to default 0please use this code
<?php $backgroundImg = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), 'full' );?>
<div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat;">
<header class="entry-header">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
</header>
I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.
if ( $post->post_parent )
$post_id = $post->post_parent;
else
$post_id = $post->ID;
if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
echo $thumbnail;
}
I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.
if ( $post->post_parent )
$post_id = $post->post_parent;
else
$post_id = $post->ID;
if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
echo $thumbnail;
}
Share
Improve this question
edited Nov 15, 2018 at 13:08
fuxia♦
107k39 gold badges255 silver badges461 bronze badges
asked Nov 15, 2018 at 6:20
William JeromeWilliam Jerome
1836 silver badges13 bronze badges
1
- 1 You can use get_the_post_thumbnail_url() for get image url and put into <div class="bg-img" style="background:PHP VARIABLE HERE"> – Pratik Patel Commented Nov 15, 2018 at 6:24
1 Answer
Reset to default 0please use this code
<?php $backgroundImg = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), 'full' );?>
<div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat;">
<header class="entry-header">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
</header>
本文标签: post thumbnailsHow to convert feature image as background image
版权声明:本文标题:post thumbnails - How to convert feature image as background image 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749178380a2328186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论