如果你製作了一個主題,需要新建很多頁面才能夠完美工作,那麼在使用者啟用主題的時候自動新建頁面將會給主題的使用省略很多設定步驟。
建立文章使用的函式為 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);
- }
- }
- }
步驟二:透過 hook 執行建立頁面函式。
有了上面的建立頁面函式,則只需要透過鉤子呼叫上面的函式即可建立頁面。注意,有的人可能使用 init 鉤子,個人認為這不是很好,init 鉤子是每次 WordPress 初始化時都要執行的,但是我們不需要每次執行程式的時候都來一遍這個函式,我們只需要在主題使用者點選啟用主題的那一刻,執行一次,以後再也不需要再執行了。所以使用 load-themes.php 鉤子,load-themes.php 鉤子是後臺在設定主題的頁面時啟用。
- 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 了,當使用者啟用你的主題的時候,可以預設建立一些必要的頁面。