Last active
August 20, 2021 17:18
-
-
Save AchalJ/ed433bc23203ce1bc20b863d9940dea4 to your computer and use it in GitHub Desktop.
ACF Gallery field - Custom Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .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