首页>文档>TranslatePress教程>自定义语言切换器

自定义语言切换器

如果您希望设计自定义的 TranslatePress 语言切换器,您可以实现一项功能来帮助您获取使用插件时所需的所有必要信息。

创建 TranslatePress 自定义语言切换器

需要调用的函数是

trp_custom_language_switcher().该函数返回具有以下参数的数组:

  • $custom_ls_array[$language_code]['language_name'] – the full language name
  • $custom_ls_array[$language_code]['language_code'] – the language code of the respective language
  • $custom_ls_array[$language_code]['short_language_name'] –  the language code of the language, the slug which will be used in the url
  • $custom_ls_array[$language_code]['flag_link'] – a link to the flag of the respective language
  • $custom_ls_array[$language_code]['current_page_url'] – the URL of the current language with the language slug added
    自定义语言切换器-HEIWP-外贸建站

    以下是定制语言切换器的示例:

    自定义语言切换器-HEIWP-外贸建站

    在这里您可以看到代码片段:

    <?php $array = trp_custom_language_switcher();  ?>
    <!-- IMPORTANT! You need to have data-no-translation on the wrapper with the links or TranslatePress will automatically translate them in a secondary language. -->    
    <ul data-no-translation>
        <!--  // Check whether TranslatePress can run on the current path or not. If the path is excluded from translation, trp_allow_tp_to_run will be false -->
        <?php if ( apply_filters( 'trp_allow_tp_to_run', true ) ){ ?>
               <?php foreach ($array as $name => $item){ ?>
                    <li style="list-style-image: url(<?php echo $item['flag_link'] ?>)"> 
                        <a href="<?php echo $item['current_page_url']?>"> 
                            <span><?php echo $item['short_language_name']. ':' . $item['language_name']?>
                            </span>
                        </a>
                    </li>
               <?php } ?>
        <?php } ?>
    </ul>

      注意:您需要在带有链接的包装器上使用

      data-no-translation ,否则 TranslatePress 会自动将它们翻译成第二语言。

      您还可以创建自定义语言切换器短代码,然后可以使用类似 [custom-language-switcher]

      /*
       * Custom language switcher shortcode
       */
      
      add_shortcode('custom-language-switcher', 'trpc_custom_language_switcher', 10);
      function trpc_custom_language_switcher(){
          // Check whether TranslatePress can run on the current path or not. If the path is excluded from translation, trp_allow_tp_to_run will be false
          if ( apply_filters( 'trp_allow_tp_to_run', true ) ){
              $languages = trp_custom_language_switcher();
              $html = "<ul data-no-translation>";
              foreach ($languages as $name => $item) {
                  $html .= "<li style='list-style-image: url({$item['flag_link']})'>";
                  $html .= "<a href='{$item['current_page_url']}'>";
                  $html .= "<span>{$item['language_name']}</span></a></li>";
              }
              $html .= "</ul>";
              return $html;
          }
      }

      如果您只需要快速创建一个经典的语言切换器,请查看TranslatePress 的默认语言切换器设置

      0 条回复 A文章作者 M管理员
        暂无讨论,说说你的看法吧
      个人中心
      购物车
      优惠劵
      搜索