0
votes

In Wordpress, I try to set the background of a div dynamically by using the post-thumbnail url in a style parameter. I put the style-parameter in a variable, named style. If I echo $style directly, it outputs:

style="background: url("http://localhost/test/wp-content/uploads/2012/11/potloden_120px.png") cover no-repeat;"

But if I echo $style in the div, like so:

<div class="column" <?php echo $style; ?>>

I get the following output:

<div class="column" style="background: url(" http:="" localhost="" test="" wp-content="" uploads="" 2012="" 11="" potloden_120px.png")="" cover="" no-repeat;"="">

Does anyone now what causes this? How should I adept my syntax for this to work?

2
localhost not working on web please change url - Vasim Shaikh

2 Answers

2
votes

I have checked, Please put below code in to your wordPress template and check it.

<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>

<div id="post" class"your-class" style="background-image: url('<?php echo $thumb['0'];?>')">
<p>text demo</p>
</div>
0
votes

Change this in your code

<div class="column" style="background: url('<?php echo $image; ?>') cover no-repeat;">

Add URL inside single quote.This will evaluate variable in proper format.

url('<?php echo $image; ?>')

output will be

<div class="column" style="background: url('http://localhost/test/wp-content/uploads/2016/11/potloden_120px.png') cover no-repeat;">

Tips Use single quotes so that you don't have to escape the double quotes (when using echo),