I'm Creating a WordPress Theme. And i have a section with the background image property in my CSS. Example
My HTML
<div class="header"></div>
My CSS
.header{
background-image:url('../path/images/image.jpg');
}
Now I want to make this section Dynamic. so the user can change their background image. Now i was trying to use post_thumbnail()
function to change my images.But i find out, that we can use that for <img/>
tag only.
Is there any way or function in wordpress that we can use to change the background image?
Do try this. It worked for me. :)
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="header" style="background-image: url('<?php echo $thumb['0'];?>')">
</div>
It's not possible in css, you have to write in php page or template.
Please see below code:
<div style="background-image: url(<?php echo get_the_post_thumbnail_url($post->ID); ?>)" class="header"></div>
Here, '$post->ID' is the ID of the post/page.
I hope, this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.