我正在创建一个自定义内容引擎,发布艺术品内容(从dropbox上传图像,从Excel电子表格中的数据构建内容,使用自定义表进行内容管理等)
我的问题是,我想发布和链接通过wordpress创建和发布的EN和ES内容。
在这里,您可以看到由于$polylang和模型对我的代码不可用而失败的代码。
// Insert the post into the database
if($record->language == \'EN\') {
if ($post_id_en = wp_insert_post($my_post, true)) {
// Insert spanish post
$post_id_es = wp_insert_post($my_post, true);
$polylang->model->set_post_language($post_id_en, \'en\');
$polylang->model->set_post_language($post_id_es, \'es\');
$polylang->model->save_translations(\'post\', $post_id_en, array(\'es\' => $post_id_es));
set_meta_data($post_id_en,$record);
set_meta_data($post_id_es,$record);
}
}
elseif($record->language == \'ES\') {
if ($post_id_es = wp_insert_post($my_post, true)) {
// Insert spanish post
$post_id_en = wp_insert_post($my_post, true);
$polylang->model->set_post_language($post_id_en, \'en\');
$polylang->model->set_post_language($post_id_es, \'es\');
$polylang->model->save_translations(\'post\', $post_id_es, array(\'en\' => $post_id_en));
set_meta_data($post_id_es,$record);
set_meta_data($post_id_en,$record);
}
}
else{
mylog("Inline: insert_post failed to find language.", "e");
}
我在init上包括Wordpress和Polylang:
require_once __DIR__ . \'/../vendor/autoload.php\';
require_once __DIR__ . \'/../../wp-load.php\';
if ( ! defined( \'ABSPATH\' ) ) {
exit; // don\'t access directly
};
require_once __DIR__ . \'/../../wp-admin/includes/upgrade.php\';
require_once __DIR__ . \'/../../wp-content/plugins/polylang/polylang.php\';
这让我可以使用所有Wordpress函数等。然而,Polylang似乎不想对我公平。
我已经尝试了通常的技巧:
global $polylang;
$polylang = new Polylang();
在阅读了文档之后,我甚至尝试将我的方法转换为使用API函数“pll\\u*”。
这种方法找到了功能和警告!!!杀死了整个网站的所有语言和翻译。
我尝试包含api函数:
require_once __DIR__ . \'/../../wp-content/plugins/polylang/include/api.php\';
require_once __DIR__ . \'/../../wp-content/plugins/polylang/polylang.php\';
然后使用:
if($record->language == \'EN\') {
if ($post_id_en = wp_insert_post($post, true)) {
$posts = array(
\'es\' => \'\'
);
// inserisco l\'articolo nelle altre lingue: En Es Fr
foreach ($posts as $language => $value) {
$posts[$language] = wp_insert_post($post, true);
}
if (function_exists(\'pll_set_post_language\')
&& function_exists(\'pll_save_post_translations\')) {
pll_set_post_language($post_id_en, \'en\');
foreach ($posts as $language => $value) {
pll_set_post_language($value, $language);
}
pll_save_post_translations(array_merge(array($post_id_en), $posts));
}
$post_id_es = $posts[\'es\'];
set_meta_data($post_id_en,$record);
set_meta_data($post_id_es,$record);
}
}
elseif($record->language == \'ES\') {
if ($post_id_es = wp_insert_post($post, true)) {
$posts = array(
\'en\' => \'\'
);
// inserisco l\'articolo nelle altre lingue: En Es Fr
foreach ($posts as $language => $value) {
$posts[$language] = wp_insert_post($post, true);
}
if (function_exists(\'pll_set_post_language\')
&& function_exists(\'pll_save_post_translations\')) {
pll_set_post_language($post_id_es, \'es\');
foreach ($posts as $language => $value) {
pll_set_post_language($value, $language);
}
pll_save_post_translations(array_merge(array($post_id_es), $posts));
}
$post_id_en = $posts[\'en\'];
set_meta_data($post_id_en,$record);
set_meta_data($post_id_es,$record);
}
}
else{
mylog("Inline: insert_post failed to find language.", "e");
}
也许它希望在调用函数之前设置默认语言或其他一些配置。