【问题标题】:Syncing custom fields between 2 languages (with WPML)在 2 种语言之间同步自定义字段(使用 WPML)
【发布时间】:2013-02-22 14:00:31
【问题描述】:

我正在使用带有自定义帖子和字段(带有高级自定义字段插件)的 WPML(Wordpress 多语言插件),我有这个“问题”: 我有一个带有自定义字段(文本)的自定义帖子,我在字段中输入文本并保存。现在我转到翻译后的帖子,看到相同的自定义字段是空的。然后字段不同步。请注意,相反,标签字段在语言之间很好地同步。 有人可以帮忙吗?谢谢

【问题讨论】:

    标签: wordpress custom-fields multilingual


    【解决方案1】:

    我认为自定义字段的保存值默认不会同步。只有变量名等。

    因此,如果您有一个自定义字段,并且不希望它在所有语言中具有相同的值,那么就不要将该自定义字段添加到其他语言中。只需将它放在主要语言上即可。

    然后在模板中你可以使用这个,总是从主要语言中获取值:

    <?php the_field('fieldname',lang_page_original_id($post->ID));?>
    

    然后将其添加到functions.php

    function lang_page_original_id($id){
        if(function_exists('icl_object_id')) {
        return icl_object_id($id,'page', false, "MAIN LANGUAGE CODE EX: SV");
        } else {
            return $id;
        }
    }
    

    【讨论】:

    • 哇!谢谢,好主意,但我还必须为语言添加自定义字段,因为有时我需要在字段中输入不同的值。 :( 其他想法?
    【解决方案2】:

    这里是 ACF 文档:http://www.advancedcustomfields.com/resources/multilingual-custom-fields/

    但它并没有你想象的那么好。同步只是从原始版本到翻译版本的“一种方式”。请参阅:https://wordpress.stackexchange.com/questions/181338/fixed-values-for-same-post-translations/214120#214120 了解更多详情。

    您需要 WPML 多语言 CMS 才能使用同步功能。

    【讨论】:

      【解决方案3】:

      您好,在您的 function.php 中使用它 100% 工作:

      function sync_field_meta( $post_id, $post, $update ) {
      
          $post_type = get_post_type($post_id);
          // use this if u have muti custom post type
          $posts_type = array('your_custom_post_type1', 'your_custom_post_type2', 'your_custom_post_type3', 'your_custom_post_type4');
      
          if( ! in_array($post_type, $posts_type)) return;
      
          $en = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'en' );
          $fr = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'fr' );
      
          // your acf key like (field_58136c9dc9963) you can check documention
          $field = get_field('acf_key',$post_id);
      
          if($en){
              update_field('acf_key',$field,$en);
          }
          if($fr){
              update_field('acf_key',$field,$fr);
          }
      
      
      }
      add_action( 'save_post', 'sync_field_meta', 10, 3 );
      

      【讨论】:

        猜你喜欢
        • 2022-01-15
        • 2022-12-03
        • 2018-01-14
        • 2014-03-12
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        相关资源
        最近更新 更多