输出翻译的分类术语名称

时间:2019-03-07 13:36:34

标签: php wordpress taxonomy wpml

我目前正在页面上输出几种语言的术语ID。我该如何为每个输出术语名称呢?

我尝试使用get_term函数来专门获得分类学术语的法语名称,即使ID是法语ID,它仍然会以英语显示。

我要去哪里错了?

  <?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 }
  }
  ?>

我正在使用WPML

1 个答案:

答案 0 :(得分:0)

希望这对其他人有帮助。我最终在每一点上都切换了语言:

  <?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 }
  }
  ?>