[Resolved] In ACF multilingual an array becomes an ID
This thread is resolved. Here is a description of the problem and solution.
Problem: The client is using ACF multilingual with WPML, and they encountered an issue where an image URL in a product category displays correctly in English, but when switching languages, the array containing the image URL becomes just a category ID in the translations. Solution: We recommended verifying the method used to call the field, suggesting the use of the approach detailed in the ACF documentation on image fields: https://www.advancedcustomfields.com/resources/image/#customized-display-array. Additionally, we provided a test site for the client to replicate the issue and further investigate the behavior of the image fields in a controlled environment. If the problem persists, we advised the client to adjust their code to check if the image field returns an ID and, if so, convert it to a URL using
wp_get_attachment_url
.
Please note that this solution might be outdated or not applicable to your specific case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the issue persists, please open a new support ticket at WPML support forum for further assistance.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
Background of the issue:
I am using ACF multilingual with WPML on this site: hidden link
There's an image URL in a product category that displays correctly in English.
However, when switching languages, the array containing the image URL becomes just a category ID in the translations.
The original array is:
Array ( [ID] => 556 [id] => 556 [title] => Pallucco Fortuny black [filename] => pallucco-fortuny-moda-LAM101-009957.jpg [filesize] => 56644 [url] => hidden link ....
(where I am collecting this variable $cat_img_loop_1['url'] , so the full url of the image)
In the translated product categories, it becomes just the ID: 556.
I expected it to remain an array, as I also selected COPY in the translation settings of ACF.
Symptoms:
The array containing the image URL becomes a simple category ID in translated product categories instead of remaining an array.
Questions:
Why is the array changed into a simple ID?
I expected the array to remain unchanged as I selected COPY in the translation settings of ACF.
Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
Hello,
Each image inside your media library has an entry in each admin language. If you go the the media library and switch the admin language to your second language, do you find an entry for the image that you are using?
If not, please go to WPML > Settings > Media Translation and click the "Start" button. This will create any missing entries inside the media library in each language.
If this will not solve the issue the field will still only return the ID instead of the whole object, then please let me know.
Which ACF Field Type are we talking about here?
Is the the Image Field and the Image Array that you refer to? hidden link
I clicked on the Start button as you suggested (I unchecked Duplicate existing media for translated content and Duplicate the featured images for translated content as I didnt want to duplicate them), but nothing has changed.
I still have the result in Englis (a complete array):
Array ( [ID] => 608 [id] => 608 [title] => Pallucco Tangent lamp [filename] => pallucco-tangent-white-low1.jpg [filesize] => 130269 [url] => hidden link ...
But still have just the ID in other languages:
608
And error: Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /var/www/vhosts/pallucco.shop/httpdocs/wp-content/themes/pallucco/inc/func-acf.php ...
I am referring to the image array, so the value of the url should be found inside:
$variable_name['url']
function acf_images_after_product_category(){
// Retrieve the current category object
$categoria = get_queried_object();
// Verify if the category object exists
if ($categoria && isset($categoria->term_id)) {
// Get the translated category ID with WPML
$categoria_id = apply_filters('wpml_object_id', $categoria->term_id, 'product_cat', true);
// Retrieve the image URL from the ACF field cat_img_loop_1
$cat_img_loop_1 = get_field('cat_img_loop_1', 'product_cat_' . $categoria_id);
// If cat_img_loop_1 is an ID, get the URL
if (is_numeric($cat_img_loop_1)) {
$cat_img_loop_1 = wp_get_attachment_url($cat_img_loop_1);
}
// Retrieve the image URL from the ACF field cat_img_loop_2
$cat_img_loop_2 = get_field('cat_img_loop_2', 'product_cat_' . $categoria_id);
// If cat_img_loop_2 is an ID, get the URL
if (is_numeric($cat_img_loop_2)) {
$cat_img_loop_2 = wp_get_attachment_url($cat_img_loop_2);
}
// Verify if both image URLs were retrieved
if ($cat_img_loop_1 && $cat_img_loop_2) {
?>
<div class="row gx-3">
<div class="col-md-6 pb-1">
<img src="<?php echo esc_url($cat_img_loop_1); ?>" alt="<?php echo esc_attr($categoria->name); ?> category image 1" class="w-100">
</div>
<div class="col-md-6 pb-1">
<img src="<?php echo esc_url($cat_img_loop_2); ?>" alt="<?php echo esc_attr($categoria->name); ?> category image 2" class="w-100">
</div>
</div>
<?php
} elseif ($cat_img_loop_1) {
?>
<div class="row">
<div class="col-12 pb-1">
<img src="<?php echo esc_url($cat_img_loop_1); ?>" alt="<?php echo esc_attr($categoria->name); ?> category image" class="w-100">
</div>
</div>
<?php
}
}
}
add_action( 'woocommerce_after_loop', 'acf_images_after_product_category' );
/* END ACF IMAGES AFTER PRODUCT CATEGORY */
/* END ACF IMAGES AFTER PRODUCT CATEGORY */
Basically adding this :
if (is_numeric($cat_img_loop_1)) {
$cat_img_loop_1 = wp_get_attachment_url($cat_img_loop_1);
}
I check if I only receive the ID i convert it to an attachment_url, I don't know if it's supposed to work like this, but in English I receive a complete URL of an image, in other languaged just the ID of the image.