Hi,
Is there any suggested code to integrate the ACF Gallery field in my posts?
many thanks,
Andrew
Hi,
Is there any suggested code to integrate the ACF Gallery field in my posts?
many thanks,
Andrew
Hi there,
Thanks for posting in.
Is it through an editor or through templates? If it’s through templates then you may check this https://www.advancedcustomfields.com/add-ons/gallery-field/
And if it’s through the editor then you have to convert the code in that sample to shortcode, example, you can add this to your child theme’s functions.php
add_shortcode('display_acf_gallery', 'acf_gallery');
function acf_gallery ( $atts ) {
if ( empty ( $atts['name'] ) ) return "";
$images = get_field( $atts['name'] );
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<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>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
}
Then you can simply add the shortcode in your content, something like this
[display_acf_gallery name="gallery"]
Hope this helps.
Hi I tried adding the code
<?php
$images = get_field('gallery');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<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>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
inside VIEWS/INTEGRITY/CONTENT.PHP
but nothing shows up for some reason… why is that?
if I use the below code, then i can see the images but they come without links to larger versions.
<?php
$image_ids = get_field(‘gallery’, false, false);
$shortcode = ‘[gallery ids="’ . implode(’,’, $image_ids) . ‘"]’;
echo do_shortcode( $shortcode );
?>
Hi there,
You have to change the gallery
from get_field('gallery');
with your down field name. It’s just a sample, it depends on what name you assign for your gallery.
And about your another code, it’s because you’re using [gallery]
shortcode, https://codex.wordpress.org/Gallery_Shortcode
link
Specify where you want the image to link. The default value links to the attachment’s permalink. Options:
file - Link directly to image file
none - No link
[gallery link=“file”]
Thanks!