【问题标题】:Wordpress Plugin auto create Parent page and childs to related parentWordpress 插件自动创建父页面和子页面到相关父页面
【发布时间】:2014-10-21 13:50:56
【问题描述】:

我开发了一个 WordPress 插件,它是一个复杂的插件,现在我想创建一个属于 Page Parent 的 Page Parent 和 Child Pages。

我的页面创建代码如下,第一个函数将创建一个父页面,第二个函数也在创建一个父页面,我无法创建第一个页面的子页面,有 post_parent 但如何获取我首先创建的那个页面父级?

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    只需在 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 2011-12-04
      • 1970-01-01
      • 2014-05-08
      • 2018-05-11
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多