Skip to content

Instantly share code, notes, and snippets.

@AchalJ
Last active August 20, 2021 17:18
Show Gist options
  • Select an option

  • Save AchalJ/ed433bc23203ce1bc20b863d9940dea4 to your computer and use it in GitHub Desktop.

Select an option

Save AchalJ/ed433bc23203ce1bc20b863d9940dea4 to your computer and use it in GitHub Desktop.
ACF Gallery field - Custom Shortcode
add_shortcode( 'custom_acf_gallery', function( $atts ) {
$gallery_data = get_field( 'gallery' ); // change the field name accordingly.
ob_start();
if ( ! empty( $gallery_data ) ) {
?>
<div class="pp-post-gallery">
<?php
foreach ( $gallery_data as $image_data ) {
$name = $image_data['filename'];
$title = empty( $image_data['title'] ) ? $name : $image_data['title'];
$alt = empty( $image_data['alt'] ) ? $title : $image_data['alt'];
$sizes = $image_data['sizes'];
$thumbnail = $sizes['thumbnail'];
$url = ! isset( $sizes['medium'] ) || empty( $sizes['medium'] ) ? $thumbnail : $sizes['medium'];
?>
<div class="pp-post-gallery-item">
<img src="<?php echo $url; ?>" alt="<?php echo esc_attr( $alt ); ?>" />
</div>
<?php
}
?>
</div>
<?php
}
return ob_get_clean();
} );
.pp-post-gallery {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment