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

  • You have to build your own shortcode. This is a copy of the acf shortcode modified to create output if not empty.

    
    function acf_shortcode_if_value( $atts ) {
      
      // extract attributs
      extract( shortcode_atts( array(
        'field'      => '',
        'post_id'    => false,
        'format_value'  => true
      ), $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 = '<p>My field value: '.$value.'</p>';
      
      // return
      return $value;
      
    }
    
    add_shortcode('acf_if_value', 'acf_shortcode_if_value');