On a Wordpress theme site I have this code:
<div class="gallery-img" style="background-image: url(<?php echo esc_url($image['sizes']['thumbnail']); ?>);">
However I need the background image url to be in ''.
So it works like this:
<div class="gallery-img" style="background-image: url('<?php echo esc_url($image['sizes']['thumbnail']); ?>');">
With it like that the PHP then does not work, how can I do the above so it works and so it has the '' around it?
How about inverting your single and double quotes ?
<div class="gallery-img" style='background-image: url("<?php echo esc_url($image['sizes']['thumbnail']); ?>");'>
Related
I'm using this code.
<div class="bg-img1 size-a-3 how1 pos-relative" style="background-image: url();">
At this time how to get the post thumbnail url in background image?
The code should be like following,
<div class="bg-img1 size-a-3 how1 pos-relative" style="background-image: url(<?php the_post_thumbnail_url(); ?>);">
And if you want to get post thumbnail from a specific post/page using post ID it will be like following,
<div class="bg-img1 size-a-3 how1 pos-relative" style="background-image: url(<?php echo get_the_post_thumbnail_url($post_id); ?>);">
I know I'm probably missing something really obvious, but the background image isn't pulling through...
<div style='background-image: url(<?php echo $blogImage; ?>");'>
I've also tried:
<div <?php echo "style='background-image: url($blogImage);'";?> >
Syntax error
Use this
<div style="background-image: url('<?php echo $blogImage; ?>');">
Hope this helps
You have a missing " before the php opening tag. try to add it and check.
<div style='background-image: url("<?php echo $blogImage; ?>");'>
My code always got this problem:
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = 'background-image: url(\''.$imgurl.'\'); ';
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;">
But whatever I do, the html always shows:
<figure id="centerbg" class="centerbg" style="background-image: url("https://cdn.jsdelivr.net/example.png"); background-position: center center; background-attachment: inherit;">
I also tried:
<figure id="centerbg" class="centerbg" style="background-image: url(<?php echo $imgurl ?>);background-position: center center;background-attachment: inherit;">
But no use at all!
Can anyone help me? TAT
Thanks for your help, but seems my and your codes all work fine in this single file:https://api.mashiro.top/cover/test.php , but always get the escape character in my production site (the same HTML id as the cover image): https://2heng.xin/ , really strange, and could be any possibility that other parts get wrong in my site?
I don't know why style="background-image: url("https://cdn.jsdelivr.net/example.jpg");" could show images in my browesr (Chrome & Firefox), but this is not a good expression ugh?
You can also just remove the single quotes on your $get_style variable.
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = 'background-image: url('.$imgurl.');';
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;height:100%;">
And it seems your site is using WordPress, so this link might help you as well.
You can mix double-quotes and single-quotes for this situation:
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = "background-image: url('".$imgurl."'); ";
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;">
Just us this.
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = "background-image: url('".$imgurl."');";
?>
You also can take a look into http://php.net/manual/en/function.htmlspecialchars-decode.php if you still face the same issue.
Try this out - I use this method a lot without issues.
<?php $imgurl = 'https://cdn.jsdelivr.net/example.png'; ?>
<figure id="centerbg" class="centerbg" style="background-image: url('<?php echo $imgurl; ?>'); background-position: center center; background-attachment: inherit;">
How can I show my image in this situation?
Here is my code:
<div class="clinic-logo" style="background-image: url( <?= base_url() ?>asset/dist/css/img/mylogo.gif )"></div>
When I saw it in inspect element it gives me this:
background-image: url( http://localhost/clinic/asset/dist/css/img/mylogo.gif );
Why does it say 'could not load image'?
Try this..
<div class="clinic-logo" style="background-image: url('<?php echo base_url();?>asset/dist/css/img/mylogo.gif')"></div>
Don't forget to load url helper in application/config/autoload.php.
should do the trick
<div class="clinic-logo" style="background-image: url("<?php echo base_url('asset/dist/css/img/mylogo.gif');?>")"></div>
anything passed into base_url() will be appended
<div class"container">
<img src="<? = base_url('Your image path')?>">
</div>
This is my code :
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
If I do
echo $img_Array["sizes"]["large"];
I get correct image path but when I use it inside background-image it does not worked for me.
When I used inspect element it displayed:
element.style {
background-image: url(<?php echo $img_Array[;
}
That means it needs to escape quotes(") of sizes , i tried to use /" but didnt work .
Any help would be appreciated . Thank you .
Try this..
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ' );">';
When you echo, you are already in PHP. No need to use <?php again.
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
Change it to:
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ');">';
It seems like you've tried to nest PHP code blocks. There are 2 possible solutions:
Echo the entire HTML code from the PHP block:
<?php
echo '<div class="banner" style="background-image: url('
. $img_Array["sizes"]["large"] . ');">';`
?>
Echo just the URL with a PHP block nested in HTML:
<div class="banner" style="background-image: url('<?php echo $img_Array["sizes"]["large"]; ?>');">