I added some custom fields to my image meta data and hid the original meta data fields (image, caption, alt text). So my new custom fields are Title, Description, Dimensions.
My template before ACF was as such:
<?php if ( is_single() ) : ?>
<h1><?php the_title(); ?></h1>
<?php else : ?>
<h1>
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
</h1>
<?php endif; // is_single() ?>
<h3><?php ffm_artists(); ?></h3>
<h3><?php date_range(); ?></h3>
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail( "large" ); ?></a>
</div>
<?php endif;
the_content('Read more...');
$images = get_field('images'); if( $images ):
foreach( $images as $image ): ?>
<figure>
<a href="<?php echo $image['url']; ?>"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
<figcaption>
<?php if($image['title'] !== '') { echo $image['title'], '<br />'; }?>
<?php if($image['caption'] !== '') { echo $image['caption'], '<br />'; }?>
<?php if($image['description'] !== '') { echo $image['description']; }?>
</figcaption>
</figure>
<?php endforeach; ?>
<?php endif; ?>
After adding the ACF fields, I changed the title, desc and dim fields to this:
foreach( $images as $image ): ?>
<figure>
<a href="<?php echo $image['url']; ?>"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
<figcaption>
<p><?php the_field('title'); ?></p>
<p><?php the_field('description'); ?></p>
<p><?php the_field('dimensions'); ?></p>
</figcaption>
</figure>
<?php endforeach; ?>
<?php endif; ?>
Now my code is just outputting blank <p><p>
where I know there is text inside the Title, Desc and Dims fields. Any thoughts?
By default, the ACF functions
the_field()
andget_field()
return custom fields directly from the current post.To use them to get fields from other places, such as taxonomy terms, options pages or – in your case – media attachments, you need to send an additional argument through.
This page in the ACF docs explains how to do it.
You need to send through the ID of the media attachment post as an additional argument. So, you’d be looking at something like this for your context: