Skip Navigation

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.

Tagged: 

This topic contains 10 replies, has 3 voices.

Last updated by Pieter 1 year, 11 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
March 8, 2023 at 8:20 pm #13201825

Pieter

Doing some customisations on the languages switcher, but running into trouble when trying to output the category name.

On category archive in default (English), category has been translated to all languages and contains 1 post.

I have added the following snippet to my functions.php:

function bhi_show_cat_link() {
	$category = get_queried_object(); // Get the current category object
	//$languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0&orderby=code' ); // Get an array of all available languages - doesn't seem to give desired output 
	$languages = icl_get_languages( 'skip_missing=0&orderby=code' ); // Get an array of all available languages

	foreach ( $languages as $language ) {
		$translated_term_id = apply_filters( 'wpml_object_id', $category->term_id, 'category', TRUE, $language['language_code'] ); // Get the translated term ID
		$translated_category = get_term( $translated_term_id, 'category' ); // Get the translated category object
		$translated_cat_title = $translated_category->name; // Get the translated category title

		echo '<a href="' . esc_url($language['url']) . '">' . $translated_cat_title . '</a><br>'; // Output the title and a link to the category archive in the current language
	}

}

And then I call that function in the footer.

The output is always the same: 4 correct links, but the category name always shows in the language where I am at.

Any idea what is wrong with the above?

March 9, 2023 at 9:30 am #13205083

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting the support.

Unfortunately we do not provide support for custom coding. However, WPML offers filters and action hooks that can be used in your WordPress theme or plugin to provide correct multilingual support. Browse following resources for more information:

- https://wpml.org/documentation/support/wpml-coding-api/
- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
- https://wpml.org/documentation/support/wpml-coding-api/shortcodes/
- https://wpml.org/faq/how-to-get-current-language-with-wpml/
- https://wpml.org/documentation/support/wpml-tables/
- https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/
- https://wpml.org/documentation/support/

If you need additional custom work, we recommend you contact WPML Contractors at https://wpml.org/contractors/.

On the other hand, remember to update WPML String Translation to latest version also, to make it compatible with WPML core plugin.

Regards.

March 9, 2023 at 9:35 am #13205181

Pieter

Yeah, very helpful, I am a Contractor. I have solved it already in another way and this clearly is a BUG.

March 9, 2023 at 9:36 am #13205187

Pieter

found an alternative solution as this seems a bug.

March 9, 2023 at 11:29 am #13206373

Pieter

OK I have set up the site as per your private message.

The function (bhi_show_cat_link() L179) has been added to the functions.php file and I have called the function in the footer.php file.

I have also added the complete function (from L194), which includes a hack to get the category name to show correctly (since I cannot get it to work as it should in the standalone snippet aboive)

Now when you go to: hidden link you will see that for the first 4 links, the link-output is correct, but the text-output is not correct. Below that is the output of my hack which shows the correct text-output.

Hope that helps.

Pieter

March 9, 2023 at 12:00 pm #13206619

Andrés
Supporter

Languages: English (English ) Spanish (Español ) French (Français )

Timezone: Europe/Paris (GMT+01:00)

Hello Pieter,

Thank you very much for your help, however could you please simplify the issue and add just the piece of code involved into a WP theme default's functions.php file? The idea is to analyse only where our hooks are failing.

As you provided us a custom theme, even if it is underscores.me, we can't go deeper. So far I have the impression that it is custom code related as Waqas pointed out initially, and it is out of the scope.

Regards,
Andrés

March 9, 2023 at 12:21 pm #13206667

Pieter

Hello Andrés,

Yes it is custom code as I am trying to output a different anchor text instead of the regular flag/language-name. From the beginning I have made clear that I am doing custom work, which is fairly normal for a WPML Contractor (https://wpml.org/contractors/bhi-consulting-for-websites/)...

Of course it has nothing to do with the theme, Underscores is as good as any, but for your argument's sake I have now used a default WP theme. No surprise, the results are exactly the same: using wpml_object_id() is not outputting the category name for the anchor text, with my creative hack it does.

I understand that you guys cannot be bothered with custom code, but this is a clear bug (or I am doing something hopelessly wrong) and I believe it would be in the interest of WPML to solve a bug.

March 9, 2023 at 12:33 pm #13206793

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for taking time to setup this in the sandbox and providing the details.

I am working on this and 'll get back to you as soon as I find something or have a solution.

Best regards.

March 10, 2023 at 6:44 am #13213775

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for your patience and cooperation.

I've done some tests and confirm the issue, which I've now escalated to the relevant team. I'll update you as soon as I hear back.

Regards.

March 13, 2023 at 6:39 am #13228361

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for your patience and cooperation.

Team suggests the following:

> get_term translate the id to current language. Try to use https://wpml.org/wpml-hook/wpml_switch_language/ instead of translating the object ID.

So I tried it on the sandbox like this:

function bhi_show_cat_link() {
    $category = get_queried_object(); // Get the current category object
    //$languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0&orderby=code' ); // Get an array of all available languages - doesn't seem to give desired output 
    $languages = icl_get_languages( 'skip_missing=0&orderby=code' ); // Get an array of all available languages
 
    foreach ( $languages as $language ) {
		do_action( 'wpml_switch_language', $language['language_code'] );
        $translated_term_id = apply_filters( 'wpml_object_id', $category->term_id, 'category', TRUE, $language['language_code'] ); // Get the translated term ID
        $translated_category = get_term( $translated_term_id, 'category' ); // Get the translated category object
        $translated_cat_title = $translated_category->name; // Get the translated category title
 
        echo '<a href="' . esc_url($language['url']) . '">' . $translated_cat_title . '</a><br>'; // Output the title and a link to the category archive in the current language
    }
 
}

And it seems to work fine (hidden link). You can apply rest of the conditional tags to control the specific WordPress query we want. See https://wpml.org/wpml-hook/wpml_switch_language/ for details on this hook.

Regards.

March 14, 2023 at 9:11 am #13240235

Pieter

Interesting to use wpml_switch_language() of which the documentation says "The action hook must be passed to the pre_get_posts WordPress hook.", which doesn't seem the case here. I assume (WooCommerce) product category can be done the same way as the above?