This thread is resolved. Here is a description of the problem and solution.
Problem: The client is experiencing an issue where custom ACF fields in the primary menu are not translating correctly on a multilingual site. While the menu items themselves translate correctly, the custom ACF fields return null when viewed in Chinese, despite displaying data correctly in English. Solution: We recommend following the steps outlined in the ACF documentation for adding and displaying fields in menus. Specifically, you can modify the
functions.php
file in your theme folder to include the following code:
add_filter('wp_nav_menu_items', 'my_wp_nav_menu_items', 10, 2);
function my_wp_nav_menu_items( $items, $args ) {
// get menu
$menu = wp_get_nav_menu_object($args->menu);
// vars
$menu_text = get_field('test_menu_field', $menu);
// append html
$items = $menu_text . $items;
// return
return $items;
}
For further guidance, refer to the ACF documentation on adding fields to menus and displaying fields. If this solution does not resolve your issue, or if the documentation links are outdated, we recommend opening a new support ticket. Additionally, please check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. If further assistance is needed, please contact our support forum.
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 working on a site under development and trying to translate custom ACF fields in the primary menu using WPML. The primary menu has been synced to Chinese, and while the menu items themselves translate correctly, the custom ACF fields do not.
Symptoms:
When viewing the page in Chinese, the custom ACF fields return null, despite working correctly and displaying data on the English (default) page.
Questions:
Why are the custom ACF fields returning null when viewing the page in Chinese, despite being correctly displayed in English?
How can I ensure that the custom ACF fields are translated and returned correctly for the Chinese menu?
I was able to set up the same system on that sandbox environment with the same result.
For simplicity, I just edited the theme file. You can find my modifications in:
Appearance > Theme File Editor > Global Templates > navbar-collapse-bootstrap5.php
I added a field to the menu called Test Menu Field in ACF. This field is attached to the Primary Menu. I then added a primary menu to the site with two placeholder links and test content in that ACF field. the then used the menu sync feature to create a French menu.
With in the theme file I called both of these fields with:
$test_fields_en = get_fields("menu_9");
$test_fields_fr = get_fields("menu_10");
I am then outputting those fields at the bottom of that file:
<h4>
Test Fields EN
</h4>
<pre>
<?php print_r($test_fields_en) ?>
</pre>
<h4>
Test Fields FR
</h4>
<pre>
<?php print_r($test_fields_fr) ?>
</pre>
On the english version of that site, I am able to output both the English and French using their menu IDs. On the French site, I get nothing: hidden link hidden link
This behavior is exactly what I am experiencing on my staging site.
I'm not sure why it doesn't work on the template file of the theme. But I just follow the sample code in the ACF documentation hidden link
to output the menu field value and it works correctly on the sandbox site. The code is added to the file functions.php in the theme folder.
add_filter('wp_nav_menu_items', 'my_wp_nav_menu_items', 10, 2);
function my_wp_nav_menu_items( $items, $args ) {
// get menu
$menu = wp_get_nav_menu_object($args->menu);
// vars
$menu_text = get_field('test_menu_field', $menu);
// append html
$items = $menu_text . $items;
// return
return $items;
}
Note: it is out of the scope of WPML. If you are not able to accomplish this, I would recommend you contact one of our certified partners who will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/
Thank you, this gave me enough to go on to get this to work for me. For whatever reason, using get_field() with the menu ID wasn't working, but using the menu object instead does!
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.