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 thread is resolved. Here is a description of the problem and solution.

Problem:
Get default language categories in all languages
Solution:
The example you provided was to retrieve posts, not categories, but it got me going to achieve what I wanted.
Here is my code:

        <?php
        global $sitepress;
        $current_lang = $sitepress->get_current_language();
        $default_lang = $sitepress->get_default_language();
        $sitepress->switch_lang($default_lang);
        $categories = get_categories();
        $sitepress->switch_lang($current_lang); //need to go back to current language here, or else "get_category" might not return what you want

        foreach($categories as $category)
        {

          $default_category = $category;
          if(!is_null($translated_category_id = icl_object_id($category->term_id, 'category', false, $current_lang)))
          { 
            $category = get_category( $translated_category_id );
          }

         //Do the stuff you want here. If no translated category was found, the default one will be used
         //You can even test for empty values on $category and use the $default_category values instead

        }
        ?>

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by pierreb-3 2 weeks, 3 days ago.

Assigned support staff: Bruno Kos.

Author Posts
September 23, 2019 at 3:33 pm #4625419

pierreb-3

Hi,

I'm trying to display a list of categories in all languages, even when they are not translated.

As per documentation and forums, I set Posts and Categories as "Translatable - use translation if available or fallback to default language"
It works fine for the blog part of the site.

I created a section that display a list of categories using this code:

foreach(get_categories('parent='.icl_object_id(15,'category')) as $category)

This works fine for different languages.

What is the link to your site?

hidden link -> the "Comporta Experiences" section, here in English there's all the categories.

hidden link -> the "Comporta Experiences" section, here in Spanish there's only one category (that is translated and has a translated article), and I would like to display all categories instead.

Kind regards,
Pierre

September 24, 2019 at 9:45 am #4629819

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Thank you for contacting WPML support!

Can you try something like this?
https://wpml.org/forums/topic/query-posts-fall-back/#post-155251

This client asked this in that ticket, so it looks to me as the same scenario you are after.
"Can I have it fall back to the default language if there is no German version?"

Regards,
Bruno Kos

September 25, 2019 at 2:14 pm #4639735

pierreb-3

Hi, thanks for your follow up.

The example you provided was to retrieve posts, not categories, but it got me going to achieve what I wanted.
Here is my code:

        <?php
        global $sitepress;
        $current_lang = $sitepress->get_current_language();
        $default_lang = $sitepress->get_default_language();
        $sitepress->switch_lang($default_lang);
        $categories = get_categories();
        $sitepress->switch_lang($current_lang); //need to go back to current language here, or else "get_category" might not return what you want

        foreach($categories as $category)
        {

          $default_category = $category;
          if(!is_null($translated_category_id = icl_object_id($category->term_id, 'category', false, $current_lang)))
          { 
            $category = get_category( $translated_category_id );
          }

         //Do the stuff you want here. If no translated category was found, the default one will be used
         //You can even test for empty values on $category and use the $default_category values instead

        }
        ?>
September 26, 2019 at 6:34 am #4643729

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Does it mean that the code you created is now working for you or you need some more assistance related to this?

Thank you for sharing it here - I will put it in this ticket summary, so that other clients can benefit from your solution :).

Regards,
Bruno Kos

September 26, 2019 at 6:52 am #4643779

pierreb-3

My issue is resolved now.