2012-01-07 76 views
1

我想要做的就是安裝一個模板,並且該模板會自動創建一個頁面,就像abotu me頁面在您首次安裝wordpress時創建的一樣。我怎樣才能做到這一點?如何在安裝新主題時運行SQL語句?因爲頁面存儲在數據庫中,所以我可以在安裝主題時以這種方式上傳頁面,但我沒有理想的方式。Wordpress關於我的頁面,在安裝新主題時複製

問候

回答

0

我上面的答案是添加帖子。下面的答案是增加一個頁面(它增加了對主題的激活頁面):

if (isset($_GET['activated']) && is_admin()){ 
    $new_page_title = 'This is the page title'; 
    $new_page_content = 'This is the page content'; 
    $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template. 
    //don't change the code bellow, unless you know what you're doing 
    $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); 
     } 
    } 
} 
+0

取自:http://wpsnipp.com/index.php/函數-php/create-page-on-theme-activation/ – JCHASE11 2012-01-07 19:23:46

+0

這樣做的工作,非常感謝! – user716495 2012-01-08 08:02:36

0

這是如何做到這一點上崗:

global $user_ID; 
$new_post = array(
'post_title' => 'My New Post', 
'post_content' => 'Lorem ipsum dolor sit amet...', 
'post_status' => 'publish', 
'post_date' => date('Y-m-d H:i:s'), 
'post_author' => $user_ID, 
'post_type' => 'post', 
'post_category' => array(0) 
); 
$post_id = wp_insert_post($new_post); 

請參閱以下頁面。請參閱this page以供參考。

+0

上面的代碼將在functions.php – JCHASE11 2012-01-07 19:19:01

+0

中看到下面的答案頁 – JCHASE11 2012-01-07 19:22:11

1

JCHASE,我wp_insert_post部分同意,我會建議的唯一的事情是使用行動掛鉤,而不是$ _GET。使用add_action('after_setup_theme','my_initiation_function')將是更好的選擇