funkjedi / acf-qtranslate

Adds qTranslate compatible fields for Text, Text Area, Wysiwyg Editor and Image.
http://wordpress.org/plugins/acf-qtranslate/
57 stars 32 forks source link

Hiding alternative content not working with ACF custom fields #124

Closed CesarNatic closed 5 years ago

CesarNatic commented 5 years ago

Hi everyone:

Im making a webpage with ACF, Advanced Custom Fields: qTranslate and qtranslate-x in english and spanish and i want to hide the content of some text fields if the text is not translated on the selected language. In the configuration of the qtranslate-x seems like there are two options that can manage this, but anyone of these makes any change if the content is on a custom field.

If the content is from a default field it works fine, shows a message saying that the content is not available in the selected language, but if i do the same with a custom field, like a text field in a ACF repeater, it shows something like: [English] xxxxxxxx, when i just want to hide the field completly. PD: Plugins are up to date and Custom fields are Qtranslate text, not basic text.

Any help is appreciated,

Thanks, César.

CesarNatic commented 5 years ago

Solved with #85 . Here is the code that worked for me:

remove_filter('get_post_metadata', 'qtranxf_filter_postmeta', 5, 4);

remove_all_filters('acf/format_value');

add_filter('acf/format_value', function($value){  
    if (is_string($value)) {
        $value = qtranxf_useCurrentLanguageIfNotFoundShowEmpty($value);
    }
    return $value;  
});

This fix have a problem and it happens when you try to put a qtranslate image or something that need a url on qtranslate acf field, the_field() returns false. So i created 3 normal ACF images and used qtrans_getlanguage function to get the correct.

<?php $lang = qtrans_getLanguage(); ?>
<?php if($lang == 'es'): ?>
<img src="<?php the_field('imagen_proceso_quees_espanol'); ?>" alt="">
<?php elseif($lang == 'en'): ?>                     
<img src="<?php the_field('imagen_proceso_quees_ingles'); ?>" alt="">
<?php endif; ?>