Support

Account

Home Forums Front-end Issues Show 'prepend' text from number field

Solved

Show 'prepend' text from number field

  • Hi I have a number field which carries a property reference eg 123 and I have set a prepend option for that field to ‘MPH’ but I now want to show the prefix on the front-end, I have tried what is suggested here: https://support.advancedcustomfields.com/forums/topic/append-and-prepend-values/ but a fatal error shows: Fatal error: Uncaught TypeError: Cannot access offset of type string on string in …

    thanks

  • do you mean the below code. There was an error that was explained by the following comment and it corrected here.

    
    $field_name = "job_field_area";
    $field = get_field_object($field_name);
    if ($field) {
      ?>
        <li class="list-item">
          <div lass="list-content">
            <h3><?php echo $field['label']; ?></h3>
            <?php echo implode(', ', wp_list_pluck($field['value'], 'name')); ?>
          </div>
        </li>
      <?php 
    }
    
  • Hi John,
    That was very helpful thanks but I would like to take it a bit further, I have made a field called ‘property_prefix’ in the options panel as a text field and then I want to update the prefix of a custom field called ‘reference’ when editing in my CPT ‘properties’ so I have done this but I can’t seem to get it to update.

    if ( is_admin() || $post_type === 'properties' ) {
    	$post_id = isset( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : - 1;
    	$field = get_field_object('reference', $post_id);
    	$fieldToUpdate = $field['prepend'];
    	$valueToUpdate = get_field('property_prefix','options');
    	
    	// echo "field: " . $fieldToUpdate . "<br />value: " . $valueToUpdate . "<br />post ID: " . $post_id; this shows the correct information
    	
    	update_field( $fieldToUpdate, $valueToUpdate, $post_id );
    	}

    I can do it by jQuery but would rather not to as when the page loads there is a delay before the prefix is shown.

    Any help would be appreciated, thanks

  • Not exactly sure what you are attempting to do.

    Are you trying to update the ACF field definition when the options page is saved?

    An ACF field cannot have different prepend values on different posts.

  • Hi John,
    I am trying to update the prepend field in a cpt edit screen with a value from the options section ‘get_field(‘property_prefix’,’options’)’. so when an admin goes to the edit screen the prepend field is populated with the prefix entered in the options screen.

    I can do it by jQuery but it stops the admin pages from working:
    echo 'jQuery(".acf-field-number .acf-input-prepend").text("'.get_field('property_prefix','options').'")';

    Any help would be appreciated thanks

  • To alter the prepend value of a form your would use an acf/load_field filter.

    Use a priority > 10 when adding the filter so that you are sure it fires after ACF’s build in filters.

    In the filter you get the option value to set the prepend value and return the field.

  • Hi John,
    Thanks for the response but thats a bit over my head, im unsure how i can update the field with that.

  • 
    add_filter('acf/load_field/name=YOUR_FIELD_NAME', 'YOUR_FUNCTION_NAME', 20);
    YOUR_FUNCTION_NAME($field) {
      $field['prepend'] = get_field('OPTION_FIELD_NAME', 'options');
      return $field;
    }
    
  • That worked great thanks 🙂

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

You must be logged in to reply to this topic.