簡體   English   中英

關於我的WordPress頁面,在安裝新主題時復制

[英]Wordpress about me page, replicate when installing new theme

我要做的是安裝一個模板,該模板會自動創建一個頁面,就像第一次安裝wordpress時創建的abotu me頁面一樣。 我怎樣才能做到這一點? 安裝新主題時如何運行SQL語句? 因為頁面存儲在數據庫中,所以我可以在安裝主題時以這種方式上載頁面,但是我沒有任何想法。

最好的祝福

JCHASE,我同意wp_insert_post部分,我建議的唯一一件事是使用動作掛鈎而不是$ _GET。 使用add_action('after_setup_theme','my_initiation_function')是一個更好的選擇

這是發布帖子的方法:

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);

請參閱下面的頁面。 請參閱此頁面作為參考。

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

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM