Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Davis Rousseau
Davis Rousseau
6,670 Points

Adding PHP to background-image url

This is what I have so far but it's not working This is what I have background-image: url(<?php the_field( 'project_images' ); ?> );

Davis Rousseau
Davis Rousseau
6,670 Points

This is what I have

.project-images {
    background-image:url('<?php the_field( 'project_images' ); ?>');
}

4 Answers

Kevin Korte
Kevin Korte
28,149 Points

I did some reformatting of you text for you so it shows up nicer. : )

You can't evaluate PHP in a CSS file. The only way to do this with PHP is to set the CSS value as an inline CSS value. Something like

<div class="product-images" style="background-image:url('<?php the_field( 'product_images'); ?>');"></div>
Davis Rousseau
Davis Rousseau
6,670 Points

Nothing is showing. Here's my site http://www.davisrousseau.com/work/pgd-media-kit-website/. thanks

element { background-image: url(''); }

Kevin Korte
Kevin Korte
28,149 Points

You may have to echo the_field out. Try

<?php echo the_field( 'product_images'); ?>

Not sure off the top of my head. Depends on how the plugins function was written. I suspect that's the case.

Davis Rousseau
Davis Rousseau
6,670 Points

Nothing.

This is how I have it set up

<div class="product-images" style="background-image:url('<?php the_field( 'product_images'); ?>');"> <?php echo the_field( 'product_images'); ?> </div>

Also I'm using custom fields

Kevin Korte
Kevin Korte
28,149 Points

You need to echo the_field where you set the URL in the style block. So it should read

I know as long as you can successfully return an image URL from your custom field you will be good.

<div class="product-images" style="background-image:url('<?php the_field( 'product_images'); ?>');"> </div>

Everything is ready to go once you can get the custom field to spit out the URL. I was playing around with it.

image