Advertisement
fauzanjeg

Example Shortcode for ACF Plugin

Jan 14th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. /* Example Shortcode for ACF Plugin */
  2. function acf_sc_image() {
  3. ?>
  4.     <!-- /* Display image (ID) || https://www.advancedcustomfields.com/resources/image/#template-usage */ -->
  5.     <?php
  6.     $image = get_field('image');
  7.     $size = 'full'; // (thumbnail, medium, large, full or custom size)
  8.     if( $image ) {
  9.         echo wp_get_attachment_image( $image, $size );
  10.     }
  11.     ?>
  12.  
  13.     <!-- /* Display image (array) || https://www.advancedcustomfields.com/resources/image/#template-usage */ -->
  14.     <?php
  15.     $image = get_field('image');
  16.     if( !empty( $image ) ): ?>
  17.         <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
  18.     <?php endif; ?>
  19.  
  20.     <!-- /* Display image (URL) || https://www.advancedcustomfields.com/resources/image/#template-usage */ -->
  21.     <?php if( get_field('image') ): ?>
  22.         <img src="<?php the_field('image'); ?>" />
  23.     <?php endif; ?>
  24. <?php
  25. }
  26.  
  27. add_shortcode('acf_image', 'acf_sc_image'); /* register shortcode [acf_image] */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement