我忍不住想我错过了什么,但似乎最明显的答案是改变你的$create_pages
阵列:
$create_pages = array(
\'one\' => \'content for one\',
\'two\' => \'content for two\',
\'three\' => \'content for three\'
);
foreach ($create_pages as $title => $content) {
$add_pages = array(
\'post_title\' => $title,
\'post_content\' => $content, // lets say, if the page_title is \'Home\', set the post_content to \'Default home page content\'
\'post_status\' => \'publish\',
\'post_type\' => \'page\'
);
var_dump($add_pages); // debugging
// $page_id = wp_insert_post($add_pages);
}
可以使用第二个帖子内容数组并将其与标题匹配,但根据您对问题的描述,我不明白您为什么需要这种复杂性。
另一种选择是使用嵌套数组,如下所示:
$create_pages = array(
array (
\'title\' => \'one\',
\'content\' => \'content for one\'
),
array (
\'title\' => \'two\',
\'content\' => \'content for two\'
),
array (
\'title\' => \'three\',
\'content\' => \'content for three\'
),
);
foreach ($create_pages as $page) {
$add_pages = array(
\'post_title\' => $page[\'title\'],
\'post_content\' => $page[\'content\'], // lets say, if the page_title is \'Home\', set the post_content to \'Default home page content\'
\'post_status\' => \'publish\',
\'post_type\' => \'page\'
);
var_dump($add_pages);
// $page_id = wp_insert_post($add_pages);
}
我很确定这两个版本都可以正常工作(参见
this 和
this), 尽管如果你有长标题,后者可能会更具可读性。
如果该数组太大,可以将其放入另一个文件中include
信息技术