Support

Account

Home Forums General Issues Shortcode: Using the Hide if empty in a shortcode Reply To: Shortcode: Using the Hide if empty in a shortcode

  • we cannot modify the shortcode in ACF. The only option we have is to build our own shortcodes. I am not the developer of ACF.

    Making changes to the shortcode I posted above is really quite simple. You just need to add the attributes you want and then use those attributes in the function. As an example, this one adds “html_before” and “html_after” which are similar attributes used in several WP functions.

    
    function acf_shortcode_if_value( $atts ) {
      
      // extract attributs
      extract( shortcode_atts( array(
        'field'      => '',
        'post_id'    => false,
        'format_value'  => true,
        'html_before' => '',
        'html_after' => ''
      ), $atts ) );
      
      
      // get value and return it
      $value = get_field( $field, $post_id, $format_value );
      
      if (empty($value)) {
        return '';
      }
      
      // array
      if( is_array($value) ) {
        
        $value = @implode( ', ', $value );
        
      }
      
      $value = $atts['html_before'].''.$value.$atts['html_after'];
      
      // return
      return $value;
      
    }
    
    add_shortcode('acf_if_value', 'acf_shortcode_if_value');