ข้ามการนำทาง

Waiting for author

Topic Tags: Compatibility

Overview of the issue

When using Grid Builder with WPML and Advanced Custom Fields (ACF), there’s a problem with Facets using custom fields. Even though the ACF field labels are translated in the backend, they are not reflected in the frontend.

Workaround

Please, make sure of having a full backup of your site before proceeding.

For Select fields

  • Open your theme’s functions.php file.
  • Add the following code:
    // WPML Workaround for wpmlsupp-11647
    function wpml_wpmlsupp11647_translate_facet_choices( $choices, $facet ) {
    
    	if ( class_exists('Sitepress') ) {
    		foreach ($choices as &$choice) {
    			$string = $choice->facet_name;
    			$textdomain = 'facets';
    			$string_name = 'Facet: '.substr($string, 0, 20);
    		
    			if ( apply_filters('wpml_default_language', NULL ) == apply_filters( 'wpml_current_language', NULL )) {
    				do_action( 'wpml_register_single_string', $textdomain, $string_name, $string );
    			}	
    			// Apply the translation to the string
    			$choice->facet_name = apply_filters('wpml_translate_single_string', $string , $textdomain, $string_name);
    		}
    	}
    
    	return $choices;
    
    }
    
    add_filter( 'wp_grid_builder/facet/choices', 'wpml_wpmlsupp11647_translate_facet_choices', 10, 2 );
    
  • Visit the page with the facets in the original language to register the strings.
  • Go to WPML > String Translation page and translate the strings.

Note: Depending on how your Facets were built, this other snippet may be helpful:

// WPML Workaround for compsupp-7453
function wpml_compsupp7453_translate_facet_block_custom_field( $output ) {
 
    if ( class_exists('Sitepress') ) {
        if( is_string($output) ) {

            $string = $output;
            $textdomain = 'facets';
            $string_name = 'Facet: '.substr($string, 0, 20);
         
            if ( apply_filters('wpml_default_language', NULL ) == apply_filters( 'wpml_current_language', NULL )) {
                do_action( 'wpml_register_single_string', $textdomain, $string_name, $string );
            }   
            // Apply the translation to the string
            $output = apply_filters('wpml_translate_single_string', $string , $textdomain, $string_name);
        }
    }
 
    return $output ;
 
}
 
add_filter( 'wp_grid_builder/block/custom_field', 'wpml_compsupp7453_translate_facet_block_custom_field', 99, 2 );

2 การตอบกลับ ถึง “Grid Builder - Facets using ACF fields labels are not translated in frontend”

  1. I haven’t had any luck with this. Is anything in the code specific to the website? Do I need to make any changes?

    I was able to get Facets to register in the string translation, and I’ve translated them, but the facets still aren’t appearing on the front end.

ทิ้งคำตอบไว้

โปรดอยู่ในหัวข้อและเคารพผู้อื่น หากคุณต้องการความช่วยเหลือเกี่ยวกับปัญหาที่ไม่เกี่ยวข้องกับโพสต์นี้ โปรดใช้ฟอรัมสนับสนุนของเราเพื่อเริ่มการสนทนาหรือส่งตั๋ว.

คุณสามารถใช้แท็กเหล่านี้:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>