Home Forums Front-end Issues Getting image url from image ID Reply To: Getting image url from image ID

  • @egr103 If you are returning an image ID then what you’re doing should work, try echoing the value to see what’s there. There is one case where it would fail, and that’s if there is no image for ‘large’ and this can happen if the uploaded image is smaller than the dimensions set for ‘large’. In other words, of large is set an 1000×1000 and you upload an image that is 800×800, there will be no large image and WP could be returning nothing.

    $hero = get_field('hero_bg');
    $size = 'large'; 
    echo 'IMAGE ID ="',$hero,'" URL = "',wp_get_attachment_image( $hero, $size ),'"';
    if( $hero ) { ?>
    <div class="c-1 hero bg-image" style="background-image: url('<?php echo wp_get_attachment_image( $hero, $size ); ?>');">
    	<div class="center-wrapper">
    		<div class="center">
    			<h1 class="cheddar"><?php the_title(); ?></h1>
    <?php } ?>