WPML

了解如何使用 wpml-config.xml 文件定义自定义字段的翻译设置。设置翻译选项(如翻译和复制),并插入其他属性。

设置自定义字段的翻译选项

WPML 为自定义字段提供四种翻译选项:

操作说明在 ATE 中可见?
翻译启用自定义字段值以进行翻译
复制将自定义字段值从默认语言复制到次要语言,同时保持两者同步
复制一次将自定义字段值从默认语言复制到次要语言,但不保持两者同步
忽略避免将自定义字段值复制到次要语言
将自定义字段添加到语言配置文件的示例
<wpml-config>
  <custom-fields>
     <custom-field action="copy">quantity</custom-field>
     <custom-field action="translate">custom-title</custom-field>
     <custom-field action="copy">weight</custom-field>
     <custom-field action="copy-once">bg-color</custom-field>
     <custom-field action="translate">custom-description</custom-field>
     <custom-field action="ignore">date-added</custom-field>
  </custom-fields>
</wpml-config>

为自定义字段添加属性

标签和分组属性

WPML 允许您添加显示在高级翻译编辑器中的属性。这包括:

  • style – 显示单行(line)、文本区域(textarea)或所见即所得(visual
  • label – 在字段旁边显示标签
  • group – 指定自定义字段所属的分组。当字段位于分组中时:
    • 该字段将从自定义字段部分中移除
    • 该字段将添加到相关分组的部分中
自定义字段属性示例
<wpml-config>
  <custom-fields>
    <custom-field action="translate" style="line" label="Title">custom-title</custom-field>
    <custom-field action="translate" style="textarea" label="Description">custom-description</custom-field>
    <custom-field action="translate" style="visual" label="Some content" group="Custom group">custom-wysiwyg</custom-field>
  </custom-fields>
</wpml-config>

以下是这些属性在高级翻译编辑器中的外观:

高级翻译编辑器中带有标签的自定义字段
高级翻译编辑器中带有标签的自定义字段

编码属性

您还可以添加可选的 encode 属性,将编码从默认值更改为其他值(默认编码)  Encoding 属性接受以下值:

  • json
  • base64
  • urlencode

如果要使用多种编码,请使用逗号分隔值:

将自定义字段添加到语言配置文件以更改编码的示例
<wpml-config>
  <custom-fields>
    <custom-field action="translate" encoding="json,base64">keywords</custom-field>
  </custom-fields>
</wpml-config>

如果您的自定义字段包含链接,您可以使用 translate_link_target 属性强制 WPML 检查并调整自定义字段中的链接。要使此属性生效,请确保链接包含在 <a> 标签内。

强制 WPML 调整自定义字段中链接的示例
<wpml-config>
  <custom-fields>
    <custom-field action="translate" translate_link_target="1">name_of_the_acf_field</custom-field>
  </custom-fields>
</wpml-config>

WPML 默认翻译自定义字段中的所有子键。要绕过此设置,请指定应翻译哪些子键。

翻译自定义字段中子键的代码示例
<wpml-config>
  <custom-fields>
    <custom-field action="translate">with_attributes</custom-field>
    <custom-field action="translate">with_deep_attributes</custom-field>
    <custom-field action="translate">no_attributes</custom-field>
  </custom-fields>
  <custom-fields-texts>
    <key name="with_attributes">
        <key name="attribute1" />
        <key name="attribute2" />
    </key>
    <key name="with_deep_attributes">
        <key name="attribute1" />
        <key name="attribute2">
          <key name="level1">
          <key name="level2">
          </key>
          </key>
        </key>
    </key>
  </custom-fields-texts>
</wpml-config>

通配符

您还可以像在管理文本中使用通配符一样使用它们:

  • 使用以下代码匹配所有以 title- 开头的子字段:
    <key name="title-*" />
  • 使用以下代码匹配所有子字段(通常用于匹配数组索引):
    <key name="*" />
  • 要获取 [{"title":"First title"},{"title":"Second title"}],请使用代码
带有通配符的配置文件示例
<wpml-config>
  <custom-fields-texts>
    <key name="key-name">
    <key name="*">
    <key name="title" />
    </key>
    </key>
  </custom-fields-texts>
</wpml-config>

自动转换文章自定义字段中的 ID

从 WPML 4.8 开始,您可以声明位于文章自定义字段中的 ID。WPML 会在前端将这些 ID 替换为其翻译版本。

只有当翻译选项为复制(即使用 <custom-field action="copy">)时,您才能执行此操作。

wpml-config.xml 文件的 custom-fields 中,使用:

  • type 声明引用的对象,即 post-ids
  • sub-type(可选)当您想要声明文章类型的别名时

WPML 可以检测并转换以下格式的 ID:

  • 单个 ID(例如 123)
  • 逗号分隔的 ID 列表(例如“123,456,789”)
  • ID 数组(以及编码数组)(例如 [123,456,789])
在自定义字段中声明 ID 的示例
<custom-fields>
	<custom-field action="copy" type="post-ids" sub-type="product">custom_field_with_id</custom-field>
</custom-fields>

当 ID 嵌套在子级别的 custom-fields-texts 部分中时,无论字段翻译设置如何(复制、翻译等),WPML 都会检测并转换它们。

在嵌套自定义字段中声明 ID 的示例
<wpml-config>
  <custom-fields-texts>
    <key name="custom_field_with_nested_ids">
      <key name="custom_field_nested_key_with_ids" type="post-ids" sub-type="product" />
      <key name="custom_field_nested_key_with_ids_with_wildcard_*" type="post-ids" sub-type="product" />
      <key name="custom_field_nested_key_with_subkey">
        <key name="custom_field_nested_key_with_subkey_with_ids" type="post-ids" sub-type="product" />
      </key>
    </key>
  </custom-fields-texts>
</wpml-config>

使用相同的 typesub-type 语法,您可以在管理文本中声明 ID(wp_options 表)。

设置自定义分类法术语字段的翻译选项

要翻译分类法术语的自定义字段,您需要指定字段名称以及 WPML 应执行的操作:translate、copy、copy-once、ignore。

将此标记嵌套在 wpml-config 标签下。

您可以为自定义术语字段设置以下翻译选项:

  • translate:允许您的用户翻译自定义术语的值。这些术语会显示在翻译编辑器屏幕上,可以手动或自动翻译,也可以发送给其他译者。
  • copy:将默认语言的自定义术语值复制到次要语言。这意味着更新默认语言的自定义术语值将始终复制到次要语言。设置为 copy 的自定义术语不会显示在翻译编辑器屏幕上。
  • copy-once:在初始翻译过程中将自定义术语的值复制到次要语言。使用 copy-once 操作的自定义术语不会显示在翻译编辑器屏幕上。但是,用户可以使用文章编辑屏幕将次要语言的自定义术语值更改为与默认语言不同。
  • ignore:避免将自定义术语复制到次要语言。
将自定义术语添加到语言配置文件的示例
<wpml-config>
  <custom-term-fields>
    <custom-term-field action="copy">term_meta_A</custom-term-field>
    <custom-term-field action="translate">term_meta_B</custom-term-field>
    <custom-term-field action="ignore">term_meta_C</custom-term-field>
    <custom-term-field action="copy-once">term_meta_D</custom-term-field>
  </custom-term-fields>
</wpml-config>

自动转换存储在分类法术语自定义字段中的 ID

只有当字段设置为复制(即使用 <custom-term-field action="copy">)时,您才能执行此操作。

与自定义文章字段一样,您可以声明分类法自定义字段中包含的对象 ID。然后 WPML 会自动将它们转换为其翻译版本。

custom-term-fields 部分中,使用:

  • type 声明引用的对象,即 post-ids 或 taxonomy-ids
  • sub-type(可选)当您想要声明特定实体时
在自定义术语字段中声明 ID 的示例
<wpml-config>
  <custom-term-fields>
    <custom-term-field action="copy" type="post-ids" sub-type="product">custom_term_field_with_id</custom-term-field>
  </custom-term-fields>
</wpml-config>

其他资源

要了解有关 wpml-config.xml 文件的更多信息,请访问语言配置文件指南。

要使用 wpml-config.xml 文件自定义其他元素,请访问我们的其他指南:

作者:Amir · 最后更新时间:2026年1月29日