检测外部链接是为了确定是否可以将 URL 更改为其他语言。如果不需要翻译某些外部链接,我们可以使用以下方法排除 TranslatePress Multilingual 检测特定域。
- 如果外部链接是使用 PHP 在服务器端生成的,则可以通过向该锚点添加属性“data-no-translation-href”将其排除在检测之外。
例如:<a href=”facebook.com” data-no-translation-href>My link</a> - 此代码将排除包含所选子文本的链接进行动态翻译。此代码仅对使用 JS 在客户端更改的链接有效。
/** * You can add a new substring to be excluded by adding in the following code a new substring in the array $excluded_substrings (see the 'example.com' below) * @param $substrings - array that contains already excluded substrings such as 'amazon-adsystem', 'googleads', 'g.doubleclick' * @return array - the array that contains all the substrings, including the ones added in this function, that will be excluded from dynamic translation */ function trpc_add_new_substring_to_be_skipped_from_dynamic_translation($substrings){ $excluded_substrings = array( 'example.com', 'another-example.com' ); // add more here $substrings['href'] = array_merge($excluded_substrings, $substrings['href']); return $substrings; } add_filter( 'trp_skip_strings_from_dynamic_translation_for_substrings', 'trpc_add_new_substring_to_be_skipped_from_dynamic_translation', 10, 1);