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.

This topic contains 8 replies, has 2 voices.

Last updated by tomaszG-4 3 months, 2 weeks ago.

Assigned support staff: Bruno.

Author Posts
June 28, 2019 at 7:01 pm #4109971

tomaszG-4

I am trying to use the following code, that I found in several threads:

function icl_post_languages() {
    $post_trid = apply_filters('wpml_element_trid', NULL, get_the_ID(), 'post_' . get_post_type());
    if (empty($post_trid))
        return;
    $translation = apply_filters('wpml_get_element_translations', NULL, $post_trid, 'post_' . get_post_type());
    if (1 < count($translation)) {
        echo __('Article disponible aussi en: ');
        foreach ($translation as $l) {
            if ($l->element_id != get_the_ID()) {
                $langs[] = '<a href="' . apply_filters('wpml_permalink', ( get_permalink($l->element_id)), $l->language_code) . '"><img src="' . ICL_PLUGIN_URL . '/res/flags/' . $l->language_code . '.png"></a>';
            }
        }
 
        echo join('  ', $langs);
    }
}

I do get the correct flags, but they link to the URL of the post of the active language.

What may be the problem?

I would also like to change from flags to translated_name in the above code - how can I achieve it?

June 28, 2019 at 10:43 pm #4110273

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Thank you for contacting us. I would be very happy to help you but I didn't fully understand what is the issue.

I have tested the code you posted above and apparently this is working properly. Are you sure the desired page is translated?

On your second question, do not you want to use the flag? Do you want to use text? Can you please elaborate a little more so I can better understand? Sometimes screenshots illustrating what you mean may go a long way to clarify what words fail to transmit so if you like make use of "upload an image" below the comment area.

Thank you.

June 29, 2019 at 5:38 am #4111131

tomaszG-4

Hello Bruno,
you are right, I wasnt very clear.
What I am trying to achieve is to list available translations for each document in a list of available documents. You can see a block from a loop on the image "loop1.jpg".
You can se there is this line: "Document also available in: {list of translations}"
But when I hover each of the links, the URL is pointing to the document in language, that it is listed in. You can see it on image "loop2.jpg" - (1) I hovered "de", but (2) I get the URL to the document in english, which is the language of the original document listed in the list.
In other words - the languages of translations are retrieved correctly, but the URLS to corresponding translations are not.
This is one issue. The other one is that instead of "de", "es", "it", I would like to get "german", "spanish", "italian" - full words.
Help much appreciated.

July 1, 2019 at 8:09 pm #4122059

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

Thank you for the detailed explanation. I saw the problem you're reporting. About the first issue, this may be happening because of some compatibility issue.

I'll need to request temporary access (wp-admin and FTP) to your site - preferably to a test site where the problem has been replicated if possible - in order to be of better help.

You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Note:

*** Please let me know exactly what steps should I follow to see the problem.

*** Please, can I install the Duplicator plugin (or All in One Migration) to make a copy of your site and install it on my test environment? This will help me debug the problem.

*** Note: Please make a full backup of your site.

Privacy and Security when Providing Debug Information for Support: https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

Thank you.

July 2, 2019 at 6:20 pm #4129369

tomaszG-4

Bruno, linking to the respective documents in other languages miraculously works, although the URLs on hover are somewhat mistaken. But it doesnt matter. The only thing, that is left is to change the "en" "de" "it" into "english" "german" "italian" - into translated_name. Ive been trying several things, but these dont work. Id appreciate hint, on what should I alter to get the desired effect.
Regards,

July 2, 2019 at 11:34 pm #4130455

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

When I use the password you posted to access the download page it appears that the password is incorrect - I'll attach an image for you to see.

By changing the code that is available on your site, you can change it by using the following code (functions.php at line 454): https://pastebin.com/EC8JUBEp

Note that I made some highlights for you to see the lines that I changed. Following these steps the language name will be displayed translated using the:

$languages[$l->language_code]['translated_name']

Please, let me know if I can help with any other questions you may have about it. I'll be happy to help.

Thank you.

July 3, 2019 at 9:32 am #4134049

tomaszG-4

Thank you veru much Bruno.
The script works now, but miraculously in some instances, all languages are listed, even for the active document, inspite the following code:

 if ($l->element_id != get_the_ID()) {

You can see it on the attached image - I added $l->element_id and get_the_ID to the loop to display the ids of the respective posts, and even though the post Libro Blanco is in spanish, it is listed in available languages. You will see next to the "Spanish" option in the "Document available also in:" line, that the IDs are different. How can that be?

July 3, 2019 at 10:01 am #4134447

tomaszG-4

I found the problem - get_the_ID() sometimes retrieved the id of the page with the listing, and not the active post. I added a short line to your function and everything works fine:

// Show linked WPML posts in a loop
function wpml_post_languages_in_loop() {
	$thispostid = get_the_ID();
    $post_trid = apply_filters('wpml_element_trid', NULL, get_the_ID(), 'post_' . get_post_type());
	$languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0&orderby=code' );
    if (empty($post_trid))
        return;
    $translation = apply_filters('wpml_get_element_translations', NULL, $post_trid, 'post_' . get_post_type());
    if (1 < count($translation)) {
        foreach ($translation as $l) {
            if ($l->element_id != $thispostid) {
                $langs[] = '<a href="' . apply_filters('wpml_permalink', ( get_permalink($l->element_id)), $l->language_code) . '">' . $languages[$l->language_code]['translated_name'] . '</a>';
				}
        }
 
        echo join(',  ', $langs);
    }
}

Thank you very much Bruno for your great help!

July 3, 2019 at 10:02 am #4134451

tomaszG-4

My issue is resolved now. Thank you!