Advertisement
fauzanjeg

Create Shortcode for ACF Gallery || Example with Wordpress Gallery

Oct 31st, 2021
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 0.74 KB | None | 0 0
  1. /* Create Shortcode for ACF Gallery || Example with Wordpress Gallery */
  2. function acf_gallery_shortcode() {
  3.     $images   = get_field( 'gallery' );
  4.     $image_ids = array();
  5.     if ( $images ) {
  6.         foreach ( $images as $image ) {
  7.             array_push( $image_ids, $image['ID'] );
  8.         }
  9.         // Generate string of ids ("123,456,789").
  10.         $images_string = implode( ',', $image_ids );
  11.  
  12.         // Generate and do shortcode.
  13.         // Note: The following string is split to simply prevent our own website from rendering the gallery shortcode.
  14.         $shortcode = sprintf( '[' . 'gallery ids="%s"]', esc_attr( $images_string ) );
  15.         echo do_shortcode( $shortcode );
  16.     }
  17. }
  18. add_shortcode( 'gallery_acf', 'acf_gallery_shortcode' ); /* Shortcode Name, Functions Name || Use [gallery_acf] */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement