Home Forums Front-end Issues get_field_object and WPML


get_field_object and WPML

  • Friends, hello. The answer was not found, I am sure that someone was able to solve this problem.

    The problem is this: I have a field with a choice of different values.
    The site is in two languages, there will be more in the future.
    In the field settings, I have created values.
    I can get

    <?php $project_scope = get_field_object( "key_12345678" )["choices"]; ?>
     <select name="project_scope" class="uk-select _req">
    <?php foreach ( $project_scope as $key => $value ): ?>
              <option value="<?php echo $key ?>"><?php echo $value ?></option>
    <?php endforeach; ?>

    And it works. But how do you make someone else choose meanings in another language?

    The key is unique and is used only for 1 field. This is true and understandable.

    I tried

    <?php//   GET all value from select FROM ACF SETTING
    $project_scope = get_field_object( acf_get_field("project_scope")["key"] )["choices"]; ?>
     <select name="project_scope" class="uk-select _req">
    	<?php foreach ( $project_scope as $key => $value ): ?>
                     <option value="<?php echo $key ?>"><?php echo $value ?></option>
    	<?php endforeach; ?>

    I still get the values from the default language. And not from which one is active.
    What parameter would I pass to get the value from this field but from another language?

  • Is hardcoding your array of options a viable solution?

    $project_scope = array(
    	'key1' => __('value1', 'text-domain'),
    	'key2' => __('value2', 'text-domain'),
    	'key3' => __('value3', 'text-domain'),

    WPML string translation could scan all values wrapped in __() and you can easily translate them.

    If you still want these values to be available in an ACF select field, you can dynamically populate that field using the same source:

    add_filter('acf/load_field/name=project_scope', 'my_project_scope_options');
    function my_project_scope_options( $field ) {
    	global $project_scope;
        $field['choices'] = $project_scope; //You can do this because it already is an associative array
        return $field;   
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.