我有一些代码可以在WordPress中创建带有子页面的页面。我想做的是让每个孩子都有自己的页面模板。正如你在下面看到的, 我正在创建脚本和资产的子页面。目前, 创建的子页面带有一个page_Full.php添加到所有内容中, 但是我希望脚本和资产具有自己的页面模板。想法会有所帮助。提前致谢。
function CreatePage(){
$post_title = $_POST['ProjectName'];
$post_excerpt = $_POST['ProjectDiscript'];
$tags = $_POST['TypeOption'];
$pages = array(
array(
'name' => $post_title, 'title' => $post_title, 'child' => array(
'script' => $post_title.'_Script', 'assets' => $post_title.'_Assets'
)
)
);
$template = array(
'post_type' => 'page', 'post_status' => 'publish', 'post_author' => $user_id, 'post_excerpt' => $post_excerpt, 'tags_input' => array($tags), );
foreach( $pages as $page ) {
$exists = get_page_by_title( $page['title'] );
$my_page = array(
'post_name' => $page['name'], 'post_title' => $page['title'], 'page_template' => 'page_Full.php'
);
$my_page = array_merge( $my_page, $template );
$id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $key, 'post_title' => $value, 'post_parent' => $id, 'page_template' => 'page_Full.php'
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
}
wp_die();
}
add_action( 'wp_ajax_CreatePage', 'CreatePage' );
#1
有为每个子页面创建一个数组的想法-并且它可以正常工作。也许还有更好的方法, 但是暂时-这可以解决问题!
$pages = array(
array(
'name' => $post_title, 'title' => $post_title, 'child' => array(
'script' => array(
'Pname' => $post_title.'_Script', 'Ptemplate' => 'page_Full_Script.php'
), 'assets' => array(
'Pname' => $post_title.'_Assets', 'Ptemplate' => 'page_Full_Assets.php'
), 'settings' => array(
'Pname' => $post_title.'_Settings', 'Ptemplate' => 'page_Full_Settings.php'
)
)
)
);
这是带有子数组的孩子。
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key[] => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $value['Pname'], 'post_title' => $value['Pname'], 'post_parent' => $id, 'page_template' => $value['Ptemplate'], );
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
这是将子数组设置为要迭代通过的var的地方, 以便可以将信息插入到child_page var中以进行合并并插入帖子。感谢所有花时间进行审查的人。干杯
评论前必须登录!
注册