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 trialDavis Rousseau
6,670 PointsAdding 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' ); ?> );
4 Answers
Kevin Korte
28,149 PointsI 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
6,670 PointsNothing is showing. Here's my site http://www.davisrousseau.com/work/pgd-media-kit-website/. thanks
element { background-image: url(''); }
Kevin Korte
28,149 PointsYou 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
6,670 PointsNothing.
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
28,149 PointsYou 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.
Davis Rousseau
6,670 Pointsgot it to work thanks
Kevin Korte
28,149 PointsAwesome, good job!
Davis Rousseau
6,670 PointsDavis Rousseau
6,670 PointsThis is what I have