我想要做的是安装一个模板,该模板自动创建一个页面,就像首次安装wordpress时创建的abotu me页面一样。我怎样才能做到这一点?安装新主题时如何运行SQL语句?由于页面存储在数据库中,我可以在安装主题时以这种方式上传页面,但我没有想法如何。
最好的问候
答案 0 :(得分:1)
JCHASE,我同意wp_insert_post部分,我建议使用动作钩子而不是$ _GET。使用add_action('after_setup_theme','my_initiation_function')将是更好的选择
答案 1 :(得分:0)
这是如何为帖子做的:
global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
请参阅下面的页面。请参阅this page以获取参考。
答案 2 :(得分:0)
我的上述答案是添加帖子。以下答案是添加页面(它添加主题激活页面):
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);
}
}
}