hopo
hopo

Reputation: 67

Wordpress : ACF image not showing

i try to display image but with Basic display (Object) but no showing

<?php 

$image = get_field('image');

if( !empty($image) ): ?>

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

<?php endif; ?>

Upvotes: 0

Views: 9157

Answers (2)

DannyTheDev
DannyTheDev

Reputation: 4173

It depends on what you set as the return value

enter image description here

  • Image Array will output as you have done
  • Image URL will return a string of the URL
  • Image ID will return the ID, so you'd need additional lookups to get the URL

If you are using this outside a loop then you must pass the post ID get_field('image', $post_id);

If it is a taxonomy term and not a post/page then you must pass a prefix get_field('image', 'category_' . $cat_id );

Upvotes: 1

Mr_DW_Brighton
Mr_DW_Brighton

Reputation: 80

From the ACF docs: http://www.advancedcustomfields.com/resources/image/

<?php 

$image = get_field('image');

if( !empty($image) ): 

	// vars
	$url = $image['url'];
	$title = $image['title'];
	$alt = $image['alt'];
	$caption = $image['caption'];

	// thumbnail
	$size = 'thumbnail';
	$thumb = $image['sizes'][ $size ];
	$width = $image['sizes'][ $size . '-width' ];
	$height = $image['sizes'][ $size . '-height' ];

	if( $caption ): ?>

		<div class="wp-caption">

	<?php endif; ?>

	<a href="<?php echo $url; ?>" title="<?php echo $title; ?>">

		<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />

	</a>

	<?php if( $caption ): ?>

			<p class="wp-caption-text"><?php echo $caption; ?></p>

		</div>

	<?php endif; ?>

<?php endif; ?>

Using Object with the image field is more powerful, as it provides access to additional data, but requires a bit more work.

You may want to change the ACF field type to Image URL, which will return just the URL of the image, and can be inserted directly into your code

Hope this helps

D

Upvotes: 0

Related Questions