WordPress编译自主建立页面
WordPress的编辑程序非常强大,所以可以自动创建页面,并编译出大量辅助网页。怎样使用WordPress主题后自主创建页面?在网页繁多时,手动创建可能会很麻烦。
废话不多说了,下面是WordPress启用主题自动创建页面的代码,加在functions.php中就行了。
//WordPress启用主题自动创建页面
function ashu_add_page($title,$slug,$parent_slug,$page_template=''){
$allPages = get_pages();//获取所有页面
$post_parent=0;
$exists = false;
foreach( $allPages as $page ){
//通过页面别名来判断页面是否已经存在
if( strtolower( $page->post_name ) == strtolower( $slug ) ){
$exists = true;
}
//获取父页面ID
if($parent_slug!=""){
if(strtolower( $page->post_name ) == strtolower( $parent_slug ) ){
$post_parent=$page->ID;
}
}
}
if( $exists == false ) {
if($post_parent!=0){
$parma= array(
'post_title' => $title,
'post_type' => 'page',
'post_name' => $slug,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_parent'=>$post_parent,
'post_author' => 1,
'menu_order' => 0
);
}else{
$parma= array(
'post_title' => $title,
'post_type' => 'page',
'post_name' => $slug,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'menu_order' => 0
);
}
$new_page_id = wp_insert_post($parma);
//如果插入成功 且设置了模板
if($new_page_id && $page_template!=''){
//保存页面模板信息
update_post_meta($new_page_id, '_wp_page_template', $page_template);
}
}
}
简单使用
在切换主题时自动检查创建页面,代码如下:
//WordPress启用主题自动创建页面
add_action( 'after_switch_theme', 'create_tb_page' );
function create_tb_page(){
ashu_add_page("用户中心","member","","pages/member.php");
ashu_add_page("修改密码","password","member","pages/member-password.php");#p#分页标题#e#
ashu_add_page("修改资料","info","member","pages/member-info.php");
ashu_add_page("积分","level","member","pages/member-level.php");
}
0条评论