如何保存不同语言的帖子,并将它们与WPML链接?

时间:2020-06-08 作者:J.BizMai

我收到一个php对象($binding_main_cpt) 从如下外部API构建:

array (
  \'post\' => 
  array (
    \'en\' => 
    array (
      \'post_title\' => \'Villa with magnificent sea view\',
      \'post_content\' => \'Located in ...\',
    ),
    \'fr\' => 
    array (
      \'post_title\' => \'Villa avec magnifique vue mer\',
      \'post_content\' => \'Située dans un domaine fermé, ...\',
    ),
  )
)
我想用WPML(高级主题插件)保存文章的翻译版本。默认语言为;fr";。

我设法插入了两篇帖子,但WPML没有识别出它们之间的链接。我试过了this solution 没有成功。

我的当前代码

Main code

global $sitepress_settings;
//Insert post with main lang
$post = $binding_main_cpt[\'post\'];
$default_language = $sitepress_settings[\'default_language\'];
$main_post = $post[$default_language];
$postInformation = [
    \'post_title\' => wp_strip_all_tags(trim($main_post[\'post_title\'] ) ),
    \'post_content\' => $main_post[\'post_content\'],
    \'post_type\' => $main_post_type,
    \'post_status\' => \'publish\'
];
$master_post_id = wp_insert_post($postInformation);
echo sprintf( "insert post %d", $master_post_id );
foreach($sitepress_settings[\'active_languages\'] as $lang){
    if( $lang !== $default_language
        && isset( $post[$lang] )
      ){
      $postInformation = [
         \'post_title\' => wp_strip_all_tags(trim($post[$lang][\'post_title\'] ) ),
         \'post_content\' => $post[$lang][\'post_content\'],
         \'post_type\' => $main_post_type,
         \'post_status\' => \'publish\'
      ];
      $new_post_id = self::wpml_translate_post( $master_post_id, $main_post_type, $lang, $postInformation );
      echo sprintf( "insert post %d", $new_post_id );
      wp_die();

    }
} 

method used for translated post

private static function wpml_translate_post( int $master_post_id, string $post_type, string $lang, array $arr_post ){

    // Include WPML API
    include_once( WP_PLUGIN_DIR . \'/sitepress-multilingual-cms/inc/wpml-api.php\' );


    // Insert translated post
    $post_translated_id = wp_insert_post(
        array(
            \'post_title\'   => $arr_post[\'post_title\'],
            \'post_type\'    => $post_type ,
            \'post_content\' => $arr_post[\'post_content\'],
            \'post_status\'  => $arr_post[\'post_status\']
        )
    );

    // Get trid of original post
    $trid = wpml_get_content_trid( \'post_\' . $post_type, $master_post_id );

    // Get default language
    $default_lang = wpml_get_default_language();

    // Associate original post and translated post
    global $wpdb;
    $wpdb->update(
        $wpdb->prefix.\'icl_translations\',
        array(
            \'trid\' => $trid,
            \'language_code\' => $lang,
            \'source_language_code\' => $default_lang
        ),
        array(
            \'element_type\' => $post_type,
            \'element_id\' => $post_translated_id
        )
    );

    // Return translated post ID
    return $post_translated_id;

}

Result on the screen

enter image description here

1 个回复
最合适的回答,由SO网友:J.BizMai 整理而成

感谢@Sally CJ我找到了解决方案:

main code

global $sitepress_settings;
//Insert post with main lang
$post = $binding_main_cpt[\'post\'];
$default_language = $sitepress_settings[\'default_language\'];
$main_post = $post[$default_language];
$postInformation = [
     \'post_title\' => wp_strip_all_tags(trim($main_post[\'post_title\'] ) ),
     \'post_content\' => $main_post[\'post_content\'],
     \'post_type\' => $main_post_type,
    \'post_status\' => \'publish\'
];
$master_post_id = wp_insert_post($postInformation);
echo sprintf( "insert post %d", $master_post_id );
foreach($sitepress_settings[\'active_languages\'] as $lang){
   if( $lang !== $default_language
       && isset( $post[$lang] )
   {
      $postInformation = [
         \'post_title\' => wp_strip_all_tags(trim($post[$lang][\'post_title\'] ) ),
         \'post_content\' => $post[$lang][\'post_content\'],
         \'post_type\' => $main_post_type,
         \'post_status\' => \'publish\'
      ];
      $new_post_id = self::wpml_translate_post( $master_post_id, $main_post_type, $lang, $postInformation );
      echo sprintf( "insert post %d", $new_post_id );
      wp_die();

   }
}

method

private static function wpml_translate_post( int $master_post_id, string $post_type, string $lang, array $arr_post ){
    global $sitepress;
    $def_trid = $sitepress->get_element_trid($master_post_id);

    // Insert translated post
    $post_translated_id = wp_insert_post(
        array(
            \'post_title\'   => $arr_post[\'post_title\'],
            \'post_type\'    => $post_type,
            \'post_content\' => $arr_post[\'post_content\'],
            \'post_status\'  => $arr_post[\'post_status\']
        )
    );

    //change language and trid of second post to match russian and default post trid
    $sitepress->set_element_language_details( $post_translated_id, \'post_\' . $post_type , $def_trid, $lang );

    // Return translated post ID
    return $post_translated_id;

}

相关推荐

通过FormData API和AJAX上传图像不起作用($_FILES始终为空)

当我尝试执行标题中提到的操作时,我的请求都能正常工作(nonce-check通过,服务器响应200已接收,服务器脚本也能正常工作)。我的问题是,我无法访问服务器端脚本上上载的图像文件$_文件始终为空。虽然已经有很多关于堆栈溢出的帖子,但他们的解决方案都不适合我。我的HTML(请注意,我不使用表单标记,但应该不会有问题):<input type=\'file\' name=\'imageupload\' id=\'image-for-upload\'> <button onclic