$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How use custom image size + ACF + background image|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How use custom image size + ACF + background image

matteradmin10PV0评论

How to get the URL from an image with custom image size to use as a background-image from AFC?

Register size in functions.php:

add_theme_support( 'post-thumbnails' );
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop

Display image with custom size (named: customsize) on page with ACF:

<?php $image = get_field('image_field'); ?>
<img src="<?php echo $image['sizes']['customsize']; ?> />

But how to display image as background image with ACF?:

<?php $image = get_field('image_field'); ?>             
<div style="background-image: url(<?php $image['url'] ?>)" /></div>

How to get the URL from an image with custom image size to use as a background-image from AFC?

Register size in functions.php:

add_theme_support( 'post-thumbnails' );
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop

Display image with custom size (named: customsize) on page with ACF:

<?php $image = get_field('image_field'); ?>
<img src="<?php echo $image['sizes']['customsize']; ?> />

But how to display image as background image with ACF?:

<?php $image = get_field('image_field'); ?>             
<div style="background-image: url(<?php $image['url'] ?>)" /></div>
Share Improve this question asked Jan 2, 2019 at 18:08 LudoLudo 612 silver badges11 bronze badges 1
  • what is the HTML output when you run your last snippet? – RiddleMeThis Commented Jan 2, 2019 at 18:14
Add a comment  | 

1 Answer 1

Reset to default 1

First off, this code...

<img src="<?php echo $image['sizes']['customsize']; ?> />

is missing the end quote, it should be...

<img src="<?php echo $image['sizes']['customsize']; ?>" />

You can use that same php to output the URL for the background size.

<div style="height: 300px; width: 300px; background-image: url(<?php echo $company_logo['sizes']['customsize']; ?>)" /></div>

Tested and works.

Post a comment

comment list (0)

  1. No comments so far