我想知道如何使用内部WPML API为帖子创建翻译(inc/wpml-api.php
)
我只想为post ID xx创建一个翻译,设置一些内容并发布它。
我试着和wpml_add_translatable_content
但我没能做对。不幸的是,没有多少文档可用于此。我找到的最接近的线索是this thread, 但我无法将代码缩减到我需要的程度。也可以通过在WPML之后直接写入数据库来实现这一点table structure, 但我想使用API。
欢迎提出任何建议。
我想知道如何使用内部WPML API为帖子创建翻译(inc/wpml-api.php
)
我只想为post ID xx创建一个翻译,设置一些内容并发布它。
我试着和wpml_add_translatable_content
但我没能做对。不幸的是,没有多少文档可用于此。我找到的最接近的线索是this thread, 但我无法将代码缩减到我需要的程度。也可以通过在WPML之后直接写入数据库来实现这一点table structure, 但我想使用API。
欢迎提出任何建议。
我想出了一个函数来完成这项工作:
/**
* Creates a translation of a post (to be used with WPML)
*
* @param int $post_id The ID of the post to be translated.
* @param string $post_type The post type of the post to be transaled (ie. \'post\', \'page\', \'custom type\', etc.).
* @param string $lang The language of the translated post (ie \'fr\', \'de\', etc.).
*
* @return the translated post ID
* */
function mwm_wpml_translate_post( $post_id, $post_type, $lang ){
// Include WPML API
include_once( WP_PLUGIN_DIR . \'/sitepress-multilingual-cms/inc/wpml-api.php\' );
// Define title of translated post
$post_translated_title = get_post( $post_id )->post_title . \' (\' . $lang . \')\';
// Insert translated post
$post_translated_id = wp_insert_post( array( \'post_title\' => $post_translated_title, \'post_type\' => $post_type ) );
// Get trid of original post
$trid = wpml_get_content_trid( \'post_\' . $post_type, $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;
}
去年,我怀着极大的兴趣关注了JonStacey的gsoc项目,结果发现这个项目只是一个有效解决方案的“基础”。http://gsoc2010.wordpress.com/tag/stream-wrappers/遗憾的是,该项目今年似乎不是gsoc的一部分,因此该项目现在有些孤立。问题:有没有人知道wordpress的php流包装器已经完成了?