我想在用户激活我的主题时创建5个页面。我从中找到一个代码wpcanyon 只能创建一个页面。根据这段代码,我如何创建5个页面而不重复5次。
if (isset($_GET[\'activated\']) && is_admin()){
$new_page_title = \'This is the page title\';
$new_page_content = \'This is the page content\';
$new_page_template = \'\'; //ex. template-custom.php. Leave blank if you don\'t want a custom page template.
//don\'t change the code bellow, unless you know what you\'re doing
$page_check = get_page_by_title($new_page_title);
$new_page = array(
\'post_type\' => \'page\',
\'post_title\' => $new_page_title,
\'post_content\' => $new_page_content,
\'post_status\' => \'publish\',
\'post_author\' => 1,
);
if(!isset($page_check->ID)){
$new_page_id = wp_insert_post($new_page);
if(!empty($new_page_template)){
update_post_meta($new_page_id, \'_wp_page_template\', $new_page_template);
}
}
}
让我知道。
SO网友:Paul
Try this:
if (isset($_GET[\'activated\']) && is_admin()){
add_action(\'init\', \'create_initial_pages\');
}
function create_initial_pages() {
$pages = array(
\'page1\' => \'Page 1\',
\'page2\' => \'Page 2\',
\'page3\' => \'Page 3\',
\'page4\' => \'Page 4\',
\'page5\' => \'Page 5\'
);
foreach($pages as $key => $value) {
$id = get_page_by_title($value);
$page = array(
\'post_type\' => \'page\',
\'post_title\' => $value,
\'post_name\' => $key,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_parent\' => \'\'
);
if (!isset($id)) wp_insert_post($page);
};
}