Support

Account

Home Forums General Issues Modify shortcode to display field label?

Solved

Modify shortcode to display field label?

  • Hi.
    I’m trying to modify the acf shortcode to also include the field label.
    The goal for this is to remove the whole field from display if the field is empty.
    Much like it’s working out of the box now with the [acf field="name"].

    So I found this thread http://support.advancedcustomfields.com/forums/topic/displaying-field-labels/ witch is part of solving this.
    But my PHP skills are rather limited 😉

    I have come up with this code. I works. But the problem here is that I have to put in the $field_name like this, $field_name = “sz_employee_experience”;

    So it’s not at all dynamic. And does not remove the label if the field is empty.

    I have tried many things but can not get it to work.
    Any help would be appreciated.

    Regards
    Olle

    function sc_headline( $atts )
    {
        // extract attributs
        extract( shortcode_atts( array(
            'field'			=> '',
    		'field_name'		=> '',
            'post_id'		=> false,
            'format_value'	=> true
        ), $atts ) );
    	
    	$field_name = "sz_employee_experience";
    	$value = get_field( $field, $post_id, $format_value);
    	$field = get_field_object($field_name);
    	
        if( is_array($value) )
        {
            $value = @implode( ', ', $value );
        }
    	return '<h3><strong>' . $field['label'] . '</strong></h3>' . $value . '<br>';
        //return $value;
    }
    add_shortcode( 'acf-headline', 'sc_headline' );
  • For the first question about putting in the field name, I don’t think there is any way around that beause that’s the only way to tell your shortcode what tag to display.

    For the second, you need to do a test of the value and if it’s empty then return noting.

    
    function sc_headline( $atts )
    {
        // extract attributs
        extract( shortcode_atts( array(
            'field'			=> '',
            'field_name'		=> '',
            'post_id'		=> false,
            'format_value'	=> true
        ), $atts ) );
    	
    	$field_name = "sz_employee_experience";
    	$value = get_field( $field, $post_id, $format_value);
    	$field = get_field_object($field_name);
    	if (!$field) {
    		return '';
    	}
    	
        if( is_array($value) )
        {
            $value = @implode( ', ', $value );
        }
    	return '<h3><strong>' . $field['label'] . '</strong></h3>' . $value . '<br>';
        //return $value;
    }
    add_shortcode( 'acf-headline', 'sc_headline' );
    
  • Hi
    Ok, but I have tested a plugin “Custom Content Shortcode”
    And with that I can set the fields field name in the shortcode like so [field field_name out=field-label]

    Much more convenient than hardcode the field name in the functions.php

    I can go with that plugin. But I’d rather not if it’s possible without another plugin.

    Regards
    Olle

  • okay, you’ve lost me, why would you hardcore the field name when you are passing the field name as part of the shortcode attributes?

    This is the code from ACF

    
    
    function acf_shortcode( $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( is_array($value) )
    	{
    		$value = @implode( ', ', $value );
    	}
    	
    	
    	return $value;
    }
    

    $field in this code is the field name.

    In your code you are resetting $field to the value returned by get_field_object.

    try something like this

    
    function sc_headline( $atts )
    {
        // extract attributs
        extract( shortcode_atts( array(
            'field'			=> '',
            'field_name'		=> '',
            'post_id'		=> false,
            'format_value'	=> true
        ), $atts ) );
    	
    	$value = get_field( $field, $post_id, $format_value);
    	$field_object = get_field_object($field);
    	if (!$field_object) {
    		return '';
    	}
    	
          if (!$value) {
     return '';
    }
        if( is_array($value) )
        {
            $value = @implode( ', ', $value );
        }
    	return '<h3><strong>' . $field['label'] . '</strong></h3>' . $value . '<br>';
        //return $value;
    }
    add_shortcode( 'acf-headline', 'sc_headline' );
    
  • That last code I posted may have had a bug.

    
      
      function sc_headline($atts) {
        // extract attributs
        extract( shortcode_atts( array(
            'field'      => '',
            'field_name'    => '',
            'post_id'    => false,
            'format_value'  => true
        ), $atts ) );
        
        $value = get_field( $field, $post_id, $format_value);
        $field_object = get_field_object($field);
      
        if (!$value) {
         return '';
        }
        if(is_array($value)) {
          $value = @implode(', ', $value);
        }
        return '<h3><strong>' . $field_object['label'] . '</strong></h3>' . $value . '<br>';
      }
      add_shortcode( 'acf-headline', 'sc_headline' );
      
    

    You’re original post asked how to not output the label if there was no value. I honestly did not understand that what you meant by “not dynamic” was that you needed to hardcode the field name and I just made it so that it would output nothing if there was no label.

  • Hi. I’m so sorry for being unclear with my question.

    In my first attempt i could get the field label to display with $field_name = “sz_employee_experience”; in the shortcode code in functions.php not in the schortcode in the text editor [acf-headline field=”sz_employee_experience”] just printed the value of the field not the field label.

    Now with your last code it works perfect! $field_object = get_field_object($field); was the thing I was after.

    Thanks a lot for being so patient and sticking by when I was so unclear.

    Regards
    Olle

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Modify shortcode to display field label?’ is closed to new replies.