Skip Navigation

[Resuelto] conditional for wpml language

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

The issue here is that the user had some custom code shortcode to get the current language for the page.

function wpml_language_code_value_func() {
    global $post;
    $my_post_language_details = apply_filters( 'wpml_post_language_details', NULL, $post->ID );
    $value = $my_post_language_details['language_code'];
    return $value;
}
add_shortcode('wpml_language_code_value', 'wpml_language_code_value_func');

They wanted to use this is a conditional code to display different content based on the language.

However when added to a conditional code the shorcode doesn't work.

Solution:

Given that this is a custom shortcode it needs to be added to the views 3rd party shortcode arguments. If the shortcode name isn't added here then it won't work inside the conditional code.

This can be found at Toolset->Settings -> Frontend.

This support ticket is created hace 5 años, 7 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Etiquetado: 

Este tema contiene 4 respuestas, tiene 3 mensajes.

Última actualización por Ido Angel hace 5 años, 7 meses.

Asistido por: Shane.

Autor
Mensajes
#1234955

hey,
I'm trying to display content (tabs in this case) conditionally according to wpml language.
I tried the solution from this post:

https://toolset.com/forums/topic/conditional-output-language/#post-402783

I added this to functions.php:

add_shortcode('get_wpml_language_code_value', 'func_wpml_language_code_value_func');
function func_wpml_language_code_value_func() {
  $return = ICL_LANGUAGE_CODE;
  return $return;
}

and am using this in the view:

<li class="single-item active" data-tab-name="Plastics Material Handling">
        [wpv-conditional if="( '[get_wpml_language_code_value]' eq 'en' )"]
        <a href="#plastics-material-handling" aria-controls="plastics-material-handling" role="tab" data-toggle="tab" class="clearfix">
                        <div class="icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <div class="content">
                            <h2>Plastics Material Handling</h2>
                          <p>Products</p>
                        </div>
                    </a>
        [/wpv-conditional]
        [wpv-conditional if="( '[get_wpml_language_code_value]' eq 'ru' )"]
        <a href="#plastics-material-handling-ru" aria-controls="plastics-material-handling" role="tab" data-toggle="tab" class="clearfix">
                        <div class="icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <div class="content">
                            <h2>Plastics Material Handling</h2>
                          <p>Products</p>
                        </div>
                    </a>
        [/wpv-conditional]
        [wpv-conditional if="( '[get_wpml_language_code_value]' eq 'es' )"]
        <a href="#plastics-material-handling-es" aria-controls="plastics-material-handling" role="tab" data-toggle="tab" class="clearfix">
                        <div class="icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <div class="content">
                            <h2>Plastics Material Handling</h2>
                          <p>Products</p>
                        </div>
                    </a>
        [/wpv-conditional]
      </li>

but that didn't work. now in all language i don't see the <a> tag at all.

any ideas?

thanks!

#1234966

What does [get_wpml_language_code_value] output in the View?
This will allow you to determine what against to check in the condition.

Also, you might try something like this:

/** 
 * Add ICL_LANGUAGE_CODE shortcode since Views ShortCode output horrible look
 * uses https://wpml.org/wpml-hook/wpml_post_language_details/
  */   
function wpml_language_code_value_func() {
    global $post;
    $my_post_language_details = apply_filters( 'wpml_post_language_details', NULL, $post->ID );
    $value = $my_post_language_details['language_code'];
    return $value;
}
add_shortcode('wpml_language_code_value', 'wpml_language_code_value_func');
#1235008

thx Beda! Unfortunately, still doesn't work.

I changed the function to yours and the view to:

      <li class="single-item active" data-tab-name="Plastics Material Handling">
        [wpv-conditional if="( '[wpml_language_code_value]' eq 'en' )"]
        <a href="#plastics-material-handling" aria-controls="plastics-material-handling" role="tab" data-toggle="tab" class="clearfix">
                        <div class="icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <div class="content">
                            <h2>Plastics Material Handling</h2>
                          <p>Products</p>
                        </div>
                    </a>
        [/wpv-conditional]
        [wpv-conditional if="( '[wpml_language_code_value]' eq 'ru' )"]
        <a href="#plastics-material-handling-ru" aria-controls="plastics-material-handling-ru" role="tab" data-toggle="tab" class="clearfix">
                        <div class="icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <div class="content">
                            <h2>Plastics Material Handling</h2>
                          <p>Products</p>
                        </div>
                    </a>
        [/wpv-conditional]
        [wpv-conditional if="( '[wpml_language_code_value]' eq 'es' )"]
        <a href="#plastics-material-handling-es" aria-controls="plastics-material-handling-es" role="tab" data-toggle="tab" class="clearfix">
                        <div class="icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <div class="content">
                            <h2>Plastics Material Handling</h2>
                          <p>Products</p>
                        </div>
                    </a>
        [/wpv-conditional]
      </li>

and I'm getting just empty

<li></li>

as if none of the conditions are met.

#1235064

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Ido,

Taking a look at this for you.

I'm assuming that your shortcode returns the correct language code.

Have you added the shortcode name to the views 3rd party shortcode arguments? If the shortcode name isn't added here then it won't work inside the conditional code.

This can be found at Toolset->Settings -> Frontend.

Please try and let me know the results.

Thanks,
Shane

#1235072

Thanks Shane!
That did the trick - but only with the first function (not Beda's).
Cheers!

Ido