Output taxonomy term names for translations

爷,独闯天下 提交于 2020-07-22 22:43:53

问题


I'm currently outputting the term id's for several languages on a page. How can I output the term names for each instead?

I've tried to use the get_term function to specifically get the French name for the taxonomy term and it still comes out in English even though the ID is the French id.

Where am I going wrong?

  <?php
  $taxonomy = "categories";
  $terms = get_terms( array(
    'suppress_filters' => false,
    'taxonomy' => $taxonomy,
    'hide_empty' => false,
  ) );

  if  ($terms) {
    foreach ($terms  as $term ) { ?>
      <p>
        EN: <?php echo $english = icl_object_id($term->term_id,'categories',false,'en'); ?><br />
        FR: <?php echo $french = icl_object_id($term->term_id,'categories',false,'fr'); ?><br />
        DE: <?php echo $german = icl_object_id($term->term_id,'categories',false,'de'); ?><br />
        IT: <?php echo $italian = icl_object_id($term->term_id,'categories',false,'it'); ?><br />
        RU: <?php echo $russian = icl_object_id($term->term_id,'categories',false,'ru'); ?><br />
        ES: <?php echo $spanish = icl_object_id($term->term_id,'categories',false,'es'); ?><br />

        <?php echo get_term(icl_object_id($french, 'categories', false, 'fr'))->name; ?>



      </p>
    <?php }
  }
  ?>

I'm using WPML.


回答1:


Hopefully this will help others. I ended up switching the language at each point:

  <?php
  global $sitepress;
  $current_lang = $sitepress->get_current_language(); //save current language
  $taxonomy = "categories";
  $terms = get_terms( array(
    'suppress_filters' => false,
    'taxonomy' => $taxonomy,
    'hide_empty' => false,
  ) );

  if  ($terms) {
    foreach ($terms  as $term ) { ?>
      <p>
        EN: <?php echo $english = apply_filters( 'wpml_object_id', $term->term_id,'categories',false,'en'); ?><br />
        FR: <?php echo $french = apply_filters( 'wpml_object_id', $term->term_id,'categories',false,'fr'); ?><br />
        DE: <?php echo $german = apply_filters( 'wpml_object_id', $term->term_id,'categories',false,'de'); ?><br />
        IT: <?php echo $italian = apply_filters( 'wpml_object_id', $term->term_id,'categories',false,'it'); ?><br />
        RU: <?php echo $russian = apply_filters( 'wpml_object_id', $term->term_id,'categories',false,'ru'); ?><br />
        ES: <?php echo $spanish = apply_filters( 'wpml_object_id', $term->term_id,'categories',false,'es'); ?><br />

        <?php $sitepress->switch_lang('en'); ?>
        EN: <?php echo get_term(icl_object_id($french, 'categories', false, 'en'))->name; ?><br />

        <?php $sitepress->switch_lang('fr'); ?>
        FR: <?php echo get_term(icl_object_id($french, 'categories', false, 'fr'))->name; ?><br />

        <?php $sitepress->switch_lang('de'); ?>
        DE: <?php echo get_term(icl_object_id($french, 'categories', false, 'de'))->name; ?><br />

        <?php $sitepress->switch_lang('it'); ?>
        IT: <?php echo get_term(icl_object_id($french, 'categories', false, 'it'))->name; ?><br />

        <?php $sitepress->switch_lang('ru'); ?>
        RU: <?php echo get_term(icl_object_id($french, 'categories', false, 'ru'))->name; ?><br />

        <?php $sitepress->switch_lang('es'); ?>
        ES: <?php echo get_term(icl_object_id($french, 'categories', false, 'es'))->name; ?><br />


        <?php $sitepress->switch_lang($current_lang); //restore previous language ?>
      </p>
      <hr />
    <?php }
  }
  ?>


来源:https://stackoverflow.com/questions/55045154/output-taxonomy-term-names-for-translations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!