簡體   English   中英

WordPress的插件自動創建父頁面和相關父級的子級

[英]Wordpress Plugin auto create Parent page and childs to related parent

我開發了一個WordPress插件,它是一個復雜的插件,現在我想創建屬於Page Parent的Page Parent和Child Pages還有什么要做。

我用於頁面創建的代碼如下,第一個函數將創建一個父頁面,第二個函數也創建一個父頁面,我無法使第一個頁面成為子對象,有post_parent但如何獲取該頁面的ID為父對象我首先創建的?

register_activation_hook( __FILE__, 'create_page_1_parent');

function create_page_1_parent()
{
    //post status and options
    $post = array(
        'comment_status' => 'closed',
        'ping_status' =>  'closed' ,
        'post_author' => 1,
        'post_date' => date('Y-m-d H:i:s'),
        'post_name' => '1first post',
        'post_status' => 'publish' ,
        'post_title' => 'parent',
        'post_type' => 'page',
        'post_parent' => '',
        'post_content' => '[il_login_form]'
    );
    //insert page and save the id
    $newvalue = wp_insert_post( $post, false );
    //save the id in the database
    update_option( 'hclpage', $newvalue );
}

register_activation_hook( __FILE__, 'create_page_1_parent_child');

function create_page_1_parent_child()
{
    //post status and options
    $post = array(
        'comment_status' => 'closed',
        'ping_status' =>  'closed' ,
        'post_author' => 1,
        'post_date' => date('Y-m-d H:i:s'),
        'post_name' => '1first post',
        'post_status' => 'publish' ,
        'post_title' => 'parent',
        'post_type' => 'page',
        'post_parent' => '', //what i have to put here that will go udner parent page
        'post_content' => '[il_login_form]'
    );
    //insert page and save the id
    $newvalue = wp_insert_post( $post, false );
    //save the id in the database

只需在1個函數中同時執行兩個插入操作即可。

function create_page_1_parent()
{
    //post status and options
    $post = array(
        'comment_status' => 'closed',
        'ping_status' =>  'closed' ,
        'post_author' => 1,
        'post_date' => date('Y-m-d H:i:s'),
        'post_name' => '1first post',
        'post_status' => 'publish' ,
        'post_title' => 'parent',
        'post_type' => 'page',
        'post_parent' => '',
        'post_content' => '[il_login_form]'
    );
    //insert page and save the id
    $newvalue = wp_insert_post( $post, false );
    //save the id in the database
    update_option( 'hclpage', $newvalue );

    //post status and options
    $post = array(
        'comment_status' => 'closed',
        'ping_status' =>  'closed' ,
        'post_author' => 1,
        'post_date' => date('Y-m-d H:i:s'),
        'post_name' => '1first post',
        'post_status' => 'publish' ,
        'post_title' => 'parent',
        'post_type' => 'page',
        'post_parent' => $newvalue, //what i have to put here that will go udner parent page
        'post_content' => '[il_login_form]'
    );
    //insert page and save the id
    $newvalue = wp_insert_post( $post, false );
    //save the id in the database
}

暫無
暫無

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

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