Support

Account

Home Forums Front-end Issues Text before field value

Solving

Text before field value

  • Hi,
    I would like to use shortcode, that would show text before the field value e.g. Name: field value, and hide both if field value is empty. where do I have to put text?
    The code is this:

    function shortcode1( $atts ){
    	return $value = get_field( "field1");
    if( $value ) {  
        echo  $value;
    } else {
        echo 'empty';
    };
    }
    add_shortcode( 'mini', 'shortcode1' );
  • Hi @ovidus

    You can insert the text just before you echo the $value like so:

    function shortcode1( $atts ){
    	return $value = get_field( "field1");
    if( $value ) {  
        echo  "Name:"." ".$value;
    } else {
        echo 'empty';
    };
    }
    add_shortcode( 'mini', 'shortcode1' );
  • Hi I tried this, but it still does not return Name: value, it is just value

  • And another question – how would in the same shorcode include several fileds so that the result would be:
    Name: value1
    Last name: value2
    Phone: value3

  • Finally this code bellow works for a single field. Any suggestion, how to include 2 or 3 fields?

    function minidatoteka( $atts ){
    	 $value = get_field( "field1");
    	if( $value ) {  
       
      return  "Name:"." ".$value;
     
    } else {
        echo 'empty';
    };
    
    	}
    add_shortcode( 'mini', 'minidatoteka' );
  • Hi @ovidus

    You can call all the fields individually within the shortcode like so:

    function minidatoteka( $atts ){
    	 $value = get_field( "field1");
    	if( $value ) {  
       
      return  "Name:"." ".$value;
     
    } else {
        echo 'empty';
    };
    $value_2 = get_field( "field2");
    	if( $value_2 ) {  
       
      return  "Last Name:"." ".$value_2;
     
    } else {
        echo 'empty';
    };
    $value_3 = get_field( "field3");
    	if( $value_3 ) {  
       
      return  "Phone:"." ".$value_3;
     
    } else {
        echo 'empty';
    };
    
    	}
    add_shortcode( 'mini', 'minidatoteka' );

    I hope this helps!

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

You must be logged in to reply to this topic.