有一个专门用于此目的的挂钩,称为after_switch_theme
. 你可以在主题激活后连接到它并创建你的个人页面。看看这个:
add_action( \'after_switch_theme\', \'create_page_on_theme_activation\' );
function create_page_on_theme_activation(){
// Set the title, template, etc
$new_page_title = __(\'About Us\',\'text-domain\'); // Page\'s title
$new_page_content = \'\'; // Content goes here
$new_page_template = \'page-custom-page.php\'; // The template to use for the page
$page_check = get_page_by_title($new_page_title); // Check if the page already exists
// Store the above data in an array
$new_page = array(
\'post_type\' => \'page\',
\'post_title\' => $new_page_title,
\'post_content\' => $new_page_content,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_name\' => \'about-us\'
);
// If the page doesn\'t already exist, create it
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);
}
}
}
我建议您使用一个独特的slug来确保代码始终正常运行,因为带有slug的页面
about-us
非常常见,可能已经存在,但不属于您的主题。