我自己想出来的。代码放在我的主题自定义函数文件中。
首先,我错误地传递了数组数据。其次,我调用函数的方式不正确。我相信有一种方法可以解决这个问题,但这是最后一段有效的代码。
$pages = array(
$page_onair => array(
\'page_id\' => -1,
\'slug\' => \'radio-shows\',
\'author_id\' => 1,
\'title\' => \'On Air Shows\',
\'tpl\' => \'template-onair.php\',
),
$page_weather => array(
\'page_id\' => -1,
\'slug\' => \'weather\',
\'author_id\' => 1,
\'title\' => \'Area Weather\',
\'tpl\' => \'template-weather.php\',
),
);
foreach($pages as $pinfo){
//print_r($pinfo);
$page_id = $pinfo[\'page_id\'];
$slug = $pinfo[\'slug\'];
$author_id = $pinfo[\'author\'];
$title = $pinfo[\'title\'];
$tpl = $pinfo[\'tpl\'];
$pnum += 1;
$programatically_create_pages = \'programatically_create_pages\' + $pnum;
$programatically_create_pages = function($page_id, $slug, $author_id, $title, $tpl) {
$page_id = $page_id;
$slug = $slug;
$author_id = $author_id;
$title = $title;
$tpl = $tpl;
//if page doesnt exist create it
if(null == get_page_by_title($title)){
//set post id so we know it was successfully created
$page_id = wp_insert_post(array(
\'comment_status\' => \'closed\',
\'ping_status\' =>\'closed\',
\'post_author\' => $author_id,
\'post_name\' => $slug,
\'post_title\' => $title,
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
)
);
update_post_meta($page_id, \'_wp_page_template\', $tpl);
} else {
// use -2 to indicate the page exists
$post_id = -2;
}
};
add_filter(\'after_setup_theme\', $programatically_create_pages($page_id, $slug, $author_id, $title, $tpl));
}