我正在使用此功能在主题激活上添加新页面:
/*************************************************
** Add home page
*************************************************/
if (isset($_GET['activated']) && is_admin()){
$new_page_title = 'Home';
$new_page_content = '';
$new_page_template = 'template-home.php';
$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);
}
}
}
使用此代码段,我想创建相当多的页面。
即
/*************************************************
** Add new pages
*************************************************/
/*************************************************
** Add home page
*************************************************/
if (isset($_GET['activated']) && is_admin()){
$new_page_title = 'Home';
$new_page_content = '';
$new_page_template = 'template-home.php';
$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);
}
}
}
/*************************************************
** Add About Us page
*************************************************/
if (isset($_GET['activated']) && is_admin()){
$new_page_title = 'About Us';
$new_page_content = 'Coming Soon';
$new_page_template = 'template-page-with-subnav.php';
$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);
}
}
}
/*************************************************
** Add Meet the Staff
*************************************************/
if (isset($_GET['activated']) && is_admin()){
$new_page_title = 'Meet the staff';
$new_page_content = 'Coming Soon';
$new_page_template = 'template-page-with-subnav.php';
$post_parent = '';
$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,
'post_parent' => $post_parent,
);
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);
}
}
}
我想知道的是如何在子页面上设置post parent?
看起来好像是后期父母设置的是&post; pp;' => [] //设置新帖子的父级(如果有)。默认为0.
但是说我正在用这种方法添加关于我们,然后想在下一个功能中添加满足工作人员,我如何得到满足工作人员的ID?
干杯
答案 0 :(得分:0)
$ new_page_id 保存刚刚创建的页面的ID(因为wp_insert_post函数的返回值是创建的帖子或页面的帖子ID)。
目前,每次创建新页面时都会覆盖此页面。
因此,每次创建页面时都可以使用新变量来存储页面ID,即每次创建页面时将$ new_page_id更改为$ about_page_id,$ home_page_id等。 E.g。
$about_page_id = wp_insert_post($new_page);
然后,为了能够设置'post_parent',您需要首先创建父页面,这样您就可以使用适当的ID来用于子页面。并添加到页面创建数组,例如
$new_page = array(
'post_type' => 'page',
'post_title' => $new_page_title,
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_author' => 1,
'post_parent' => $about_page_id
);