我收到一个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
最合适的回答,由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;
}