Syntax on this line

Hi there,

I am having some issues with this line and can’t get the syntax correct:

echo "<div class='test-div' style='background-image: url( the_field(\"background_image\"));'>";

I basically need to get the background-image to display the value of the_field("background_image")

Does anyone know what I have done wrong?

Thanks

I think your PHP is doing what you intended but

is not a valid property value. It should be an image, along with its path.

is the_field a php function? a javascript function?

It’s a PHP function from a WordPress plugin.

This is the documentation:

I also tried this, but it doesn’t display the image:

 <?php if( get_field('background_image') ): ?>
    <img src="<?php the_field('background_image'); ?>" />
<?php endif; ?>

Terminate your echo string and concatenate the function to it.

echo "<div class='test-div' style='background-image: url( the_field(\"background_image\"));'>";

=>

echo "<div class='test-div' style='background-image: url(".the_field("background_image").");'>";

Many thanks.

That seems to be outputting this on the front end:

style="background-image: url();">

I must have something else missing that’s not finding the field value

I found out I was missing some parts.

I don’t think it knew where to get the image from.

This is what I ended up with:

<div class='test-div 'style="background-image: url(<?php echo get_field('promo_background_image', $taxonomy.'_'.$term_id); ?>);">