Home › Forums › Front-end Issues › gallery Array
I made a Field Group which includes a gallery
I use the code below in single.php, to show the fields
everything is ok except the gallery which in front end show Array
in back end is ok, I can upload pictures
you have an idea what’s going on?
Thank you
the cod
<?php
$fields = get_fields($post->ID);
if( $fields ): ?>
<ul>
<?php foreach( $fields as $name => $value ): ?>
<li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i have support but …
A gallery field always returns an array of images/image data. An explanation of this is in the documentation https://www.advancedcustomfields.com/resources/gallery/.
A gallery field is one of the more advanced field types. The values of these fields cannot be simply echoed on the page, and they are not really appropriate for use with get_fields()
and looping over all the fields that are returned. In such a loop you would need to test for the different fields and do something different with these special fields.
<?php
$fields = get_fields($post->ID);
if ($fields) {
?>
<ul>
<?php
foreach ($fields as $name => $value) {
?>
<li>
<b><?php echo $name; ?></b>
<?php
if ($name == 'THE_NAME_OF_YOUR_GALLERY_FIELD') {
// do something special for gallery fields
} else {
// basic field
echo $value;
}
?>
</li>
<?php
} // end foreach fields
?>
</ul>
<?php
} // end if fields
?>
I do not really understand
besides replacing it THE_NAME_OF_YOUR_GALLERY_FIELD with the name of the gallery field, album, something else to be done?
I do not understand why it is so complicated
it does not display any existing or uploaded photos
<?php
$fields = get_fields($post->ID);
if ($fields) {
?>
<ul>
<?php
foreach ($fields as $name => $value) {
?>
<li>
<b><?php echo $name; ?></b>
<?php
if ($name == 'album') {
// do something special for gallery fields
} else {
// basic field
echo $value;
}
?>
</li>
<?php
} // end foreach fields
?>
</ul>
<?php
} // end if fields
?>
if in the place of a generic code i uses individual codes of the kind
<p><strong> Location of copies: </p></strong> <?php the_field( 'location_of_copies', $post->ID ); ?>
which is the gallery code that work with a field of gellery name album?
As I said, the gallery field is one of the more complicated fields.
Yes, you would replace THE_NAME_OF_YOUR_GALLERY_FIELD
with the name of your gallery field and then where it says // do something special for gallery fields
you would add code that output the gallery the way you want.
Did you look at the link I gave you? There are several examples of code for showing a gallery in different ways. https://www.advancedcustomfields.com/resources/gallery/
i dont know php so is hard 🙂
replace
// do something special for gallery fields
with
<?php
$image_ids = get_field('gallery', false, false);
$shortcode = '[' . 'gallery ids="' . implode(',', $image_ids) . '"]';
echo do_shortcode( $shortcode );
?>
or
$image_ids = get_field('gallery', false, false);
$shortcode = '[' . 'gallery ids="' . implode(',', $image_ids) . '"]';
echo do_shortcode( $shortcode );
in this code do I have to replace something?
The topic ‘gallery Array’ 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.