Home › Forums › Add-ons › Gallery Field › Display Field Label For Gallery Add-on
Hello, I am trying to automatically display the Field Label for my text fields and for my Gallery Field.
Here is a link to the site I am working on – http://50.62.44.173/~dolcearabians/a-special-versace/
Here is the code I have currently placed in my hooks to display the 4 custom fields:
<div class="acfblock">
<br style="clear:both"/> <br>
<p>
<h1 class="entry-title"> Pedigree </h1>
<p><?php the_field('horse_pedigree'); ?></p>
<br style="clear:both"/>
<h1 class="entry-title"> Photo Gallery </h1>
<?php
$images = get_field('gallery');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<div class="aligngallery">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<br style="clear:both"/>
<h1 class="entry-title"> Offspring </h1>
<?php
$images = get_field('offspring');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<div class="alignoffspring">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<br style="clear:both"/>
<h1 class="entry-title"> Video </h1>
<p><?php the_field('video'); ?></p> </div>
As you can see I am currently using <h1> tags to create the titles, but this is not a good solution as the field titles display even if the user has not inputted any gallery or video content.
Can you help me with what code I need to display a field label for my Gallery fields (Gallery & Offspring) and also for my text fields? (Pedigree, Video)
Your help is greatly appreciated.
Hi @makeitminnesota,
Thank you for the question.
You can make use of the get_field_obejct function to return an array of the field object as seen in the field group edit screen.
In your scenario, you can apply the function as follow:
/*
* Get a field object and display it with it's value (using the field key and the value fron another post)
*/
$field_key = "field_5039a99716d1d";
$post_id = 123;
$field = get_field_object($field_key, $post_id);
echo $field['label'] . ': ' . $field['value'];
You can have a look at this resource page for more info on this.
The topic ‘Display Field Label For Gallery Add-on’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.