Gallery Field
The Gallery field allows selecting and ordering multiple images from the WordPress media library. It returns an array of attachment IDs in the order set by the user.
Field Settings
| Setting | Description |
|---|---|
| Field Key | Unique identifier. Lowercase letters and underscores only. |
| Label | Display label shown above the field. |
| Description | Optional helper text shown below the field. |
| Instructions | Optional instructions shown above the upload area. |
Return Value
Returns an array of integers — the WordPress attachment IDs in the user-defined order. Returns an empty array [] if no images are selected.
Always check
is_array()before looping.
Usage
php
// Get the attachment IDs (returns array)
$gallery = onemeta_get_meta( get_the_ID(), 'field_key' );
if ( is_array( $gallery ) && ! empty( $gallery ) ) {
echo '<div class="gallery">';
foreach ( $gallery as $image_id ) {
echo wp_get_attachment_image( $image_id, 'medium', false, [
'class' => 'gallery-item',
] );
}
echo '</div>';
}
Template Example
php
<?php $photos = onemeta_get_meta( get_the_ID(), 'project_gallery' ); ?>
<?php if ( is_array( $photos ) && ! empty( $photos ) ) : ?>
<div class="project-gallery">
<?php foreach ( $photos as $image_id ) : ?>
<?php $full_url = wp_get_attachment_image_url( $image_id, 'full' ); ?>
<a href="<?php echo esc_url( $full_url ); ?>" class="gallery-link">
<?php echo wp_get_attachment_image( $image_id, 'thumbnail', false, [
'class' => 'gallery-thumb',
'alt' => esc_attr( get_post_meta( $image_id, '_wp_attachment_image_alt', true ) ),
] ); ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
Notes
- Returns an array of attachment IDs, not URLs
- Use
wp_get_attachment_image()for each ID to get a properly formed<img>tag with srcset - Images are returned in the order set by the user in the media library picker
- Use with a lightbox plugin like Zoomora for a full gallery experience