Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created January 2, 2017 16:24
Show Gist options
  • Select an option

  • Save morgyface/e58d3b385688837085185eb07af2a066 to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/e58d3b385688837085185eb07af2a066 to your computer and use it in GitHub Desktop.
WordPress | Using the ACF Image field to apply a background
<style>
div.panel {width:100%; height:100vh; background-position:center center; background-repeat:no-repeat; -webkit-background-size:cover; -moz-background-size:cover; -o-background-size:cover; background-size:cover}
</style>
<?php
// Image field with a return value of Image Array
$background_image = get_field('background_image');
echo '<div class="panel"';
if ( $background_image ) {
$size = 'large';
$large_bg_url = $background_image['sizes'][ $size ];
echo ' style="background-image: url(' . $large_bg_url . ')"';
}
echo '>' . PHP_EOL;
?>
@EvenStevenSwe
Copy link

Hi, I'm struggling with ACF, although it's supposed to be so easy. What you have created here seems a bit easier than other guides I've found before, but still I have to ask. Where do I declare where the path to the image? I'd really appeciate an answer, thanks!

@morgyface
Copy link
Author

Hey @EvenStevenSwe, sorry it's taken me 4 days to get back to you, I clearly need to mess with my notifications! Let me know a bit more about what you're trying to achieve and I'll help as best I can. In this example the path to the image is generated by $background_image['sizes'][ 'large' ];. But that's because we're using an array return value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment