> WordPress中文手册 > wordpress进阶教程(三十二): 在激活主题的时候自动新建页面

如果你制作了一个主题,需要新建很多页面才能够完美工作,那么在使用者激活主题的时候自动新建页面将会给主题的使用省略很多设置步骤。

创建文章使用的函数为wp_insert_post();使用方法如下

<?php $post = array( 'ID' => [ <post id> ] //Are you updating an existing post? 'menu_order' => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs. 'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 'ping_status' => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off 'pinged' => [ ? ] //? 'post_author' => [ <user ID> ] //The user ID number of the author. 'post_category' => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories 'post_content' => [ <the text of the post> ] //The full text of the post. 'post_date' => [ Y-m-d H:i:s ] //The time post was made. 'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT. 'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs. 'post_name' => [ <the name> ] // The name (slug) for your post 'post_parent' => [ <post ID> ] //Sets the parent of the new post. 'post_password' => [ ? ] //password for post? 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post. 'post_title' => [ <the title> ] //The title of your post. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type 'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 'to_ping' => [ ? ] //? 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. ); wp_insert_post( $post, $wp_error ); /* $wp_error参数 布尔值 如果出错允许返回一个类。 如果插入文章成功,函数将会返回插入的文章ID,如果出错,$wp_error设置为true 则返回一个类,否则返回0 */ ?> 需要注意,页面的模板信息保存在_postmeta表中,以字段形式保存,字段名为_wp_page_template,所以要保存页面模板信息,使用update_post_meta函数 /** *参数$title 字符串 页面标题 *参数$slug 字符串 页面别名 *参数$page_template 字符串 模板名 *无返回值 **/ function ashu_add_page($title,$slug,$page_template=''){ $allPages = get_pages();//获取所有页面 $exists = false; foreach( $allPages as $page ){ //通过页面别名来判断页面是否已经存在 if( strtolower( $page->post_name ) == strtolower( $slug ) ){ $exists = true; } } if( $exists == false ) { $new_page_id = wp_insert_post( 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 ) ); //如果插入成功 且设置了模板 if($new_page_id && $page_template!=''){ //保存页面模板信息 update_post_meta($new_page_id, '_wp_page_template', $page_template); } } } function ashu_add_pages() { global $pagenow; //判断是否为激活主题页面 if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){ ashu_add_page('ASHU_PAGE','ashu-page','page-ashu.php'); //页面标题ASHU_PAGE 别名ashu-page 页面模板page-ashu.php ashu_add_page('PAGE_ASHU','page-ashu','ashu-page.php'); } } add_action( 'load-themes.php', 'ashu_add_pages' ); //需要注意的是模板名称是PHP文件的文件名哦

步骤一:添加页面的函数

<?php $post = array( 'ID' => [ <post id> ] //Are you updating an existing post? 'menu_order' => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs. 'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 'ping_status' => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off 'pinged' => [ ? ] //? 'post_author' => [ <user ID> ] //The user ID number of the author. 'post_category' => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories 'post_content' => [ <the text of the post> ] //The full text of the post. 'post_date' => [ Y-m-d H:i:s ] //The time post was made. 'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT. 'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs. 'post_name' => [ <the name> ] // The name (slug) for your post 'post_parent' => [ <post ID> ] //Sets the parent of the new post. 'post_password' => [ ? ] //password for post? 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post. 'post_title' => [ <the title> ] //The title of your post. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type 'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 'to_ping' => [ ? ] //? 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. ); wp_insert_post( $post, $wp_error ); /* $wp_error参数 布尔值 如果出错允许返回一个类。 如果插入文章成功,函数将会返回插入的文章ID,如果出错,$wp_error设置为true 则返回一个类,否则返回0 */ ?> 需要注意,页面的模板信息保存在_postmeta表中,以字段形式保存,字段名为_wp_page_template,所以要保存页面模板信息,使用update_post_meta函数 /** *参数$title 字符串 页面标题 *参数$slug 字符串 页面别名 *参数$page_template 字符串 模板名 *无返回值 **/ function ashu_add_page($title,$slug,$page_template=''){ $allPages = get_pages();//获取所有页面 $exists = false; foreach( $allPages as $page ){ //通过页面别名来判断页面是否已经存在 if( strtolower( $page->post_name ) == strtolower( $slug ) ){ $exists = true; } } if( $exists == false ) { $new_page_id = wp_insert_post( 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 ) ); //如果插入成功 且设置了模板 if($new_page_id && $page_template!=''){ //保存页面模板信息 update_post_meta($new_page_id, '_wp_page_template', $page_template); } } } function ashu_add_pages() { global $pagenow; //判断是否为激活主题页面 if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){ ashu_add_page('ASHU_PAGE','ashu-page','page-ashu.php'); //页面标题ASHU_PAGE 别名ashu-page 页面模板page-ashu.php ashu_add_page('PAGE_ASHU','page-ashu','ashu-page.php'); } } add_action( 'load-themes.php', 'ashu_add_pages' ); //需要注意的是模板名称是php文件的文件名哦

步骤二:通过hook执行创建页面函数。

有了上面的创建页面函数,则只需要通过钩子调用上面的函数即可创建页面。注意,有的人可能使用init钩子,个人认为这不是很好,init钩子是每次WordPress初始化时都要执行的,但是我们不需要每次执行程序的时候都来一遍这个函数,我们只需要在主题使用者点击激活主题的那一刻,执行一次,以后再也不需要再执行了。所以使用load-themes.php钩子,load-themes.php钩子是后台在设置主题的页面时启用。

<?php $post = array( 'ID' => [ <post id> ] //Are you updating an existing post? 'menu_order' => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs. 'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 'ping_status' => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off 'pinged' => [ ? ] //? 'post_author' => [ <user ID> ] //The user ID number of the author. 'post_category' => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories 'post_content' => [ <the text of the post> ] //The full text of the post. 'post_date' => [ Y-m-d H:i:s ] //The time post was made. 'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT. 'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs. 'post_name' => [ <the name> ] // The name (slug) for your post 'post_parent' => [ <post ID> ] //Sets the parent of the new post. 'post_password' => [ ? ] //password for post? 'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post. 'post_title' => [ <the title> ] //The title of your post. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type 'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 'to_ping' => [ ? ] //? 'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. ); wp_insert_post( $post, $wp_error ); /* $wp_error参数 布尔值 如果出错允许返回一个类。 如果插入文章成功,函数将会返回插入的文章ID,如果出错,$wp_error设置为true 则返回一个类,否则返回0 */ ?> 需要注意,页面的模板信息保存在_postmeta表中,以字段形式保存,字段名为_wp_page_template,所以要保存页面模板信息,使用update_post_meta函数 /** *参数$title 字符串 页面标题 *参数$slug 字符串 页面别名 *参数$page_template 字符串 模板名 *无返回值 **/ function ashu_add_page($title,$slug,$page_template=''){ $allPages = get_pages();//获取所有页面 $exists = false; foreach( $allPages as $page ){ //通过页面别名来判断页面是否已经存在 if( strtolower( $page->post_name ) == strtolower( $slug ) ){ $exists = true; } } if( $exists == false ) { $new_page_id = wp_insert_post( 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 ) ); //如果插入成功 且设置了模板 if($new_page_id && $page_template!=''){ //保存页面模板信息 update_post_meta($new_page_id, '_wp_page_template', $page_template); } } } function ashu_add_pages() { global $pagenow; //判断是否为激活主题页面 if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){ ashu_add_page('ASHU_PAGE','ashu-page','page-ashu.php'); //页面标题ASHU_PAGE 别名ashu-page 页面模板page-ashu.php ashu_add_page('PAGE_ASHU','page-ashu','ashu-page.php'); } } add_action( 'load-themes.php', 'ashu_add_pages' ); //需要注意的是模板名称是php文件的文件名哦

好了,这样就OK了,当使用者激活你的主题的时候,可以默认创建一些必要的页面。