如何以编程方式创建主题激活的“子”页面?

时间:2011-10-16 作者:Paul

我知道如何在激活主题时自动创建页面,但我需要帮助来了解如何同时以编程方式创建子页面。

例如:

- Page 1
  - Page 1.1
  - Page 1.2
- Page 2
  - Page 2.1
  - Page 2.2
  - Page 2.3
- Page 3
  - Page 3.1
- Page 4
  - Page 4.1
  - Page 4.2
这是我的自动页面创建功能(仅限顶层)。

if (isset($_GET[\'activated\']) && is_admin()){
    add_action(\'init\', \'create_initial_pages\');
}

function create_initial_pages() {
    $pages = array(
        \'page1\' => \'Page 1\',
        \'page2\' => \'Page 2\',
        \'page3\' => \'Page 3\',
        \'page4\' => \'Page 4\'
    );
    foreach($pages as $key => $value) {
        $id = get_page_by_title($value);
        $page = array(
            \'post_type\'   => \'page\',
            \'post_title\'  => $value,
            \'post_name\'   => $key,
            \'post_status\' => \'publish\',
            \'post_author\' => 1,
            \'post_parent\' => \'\'
        );
        if (!isset($id)) wp_insert_post($page);
    };
}
是否有人知道如何扩展此功能,以便在激活主题时也创建子页面(如果它们不存在)?

2 个回复
最合适的回答,由SO网友:Paul 整理而成

@Tareq的示例非常有用,但它不会为父页面创建多个子页面,而是将每个子页面都变成子父页面。

Page 1
- Page 1.1
  -- Page 1.2
     --- Page 1.3
Page 2
- Page 2.1
  -- Page 2.2

etc.
以下是修复/改进的功能(我相信这可以得到进一步改进,因为页面检查不够彻底):

function create_initial_pages() {
    $pages = array(
        array(
            \'name\'  => \'page1\',
            \'title\' => \'Page 1\',
            \'child\' => array(
                \'page1-1\' => \'Page 1.1\',
                \'page1-2\' => \'Page 1.2\',
                \'page1-3\' => \'Page 1.3\',
                \'page1-4\' => \'Page 1.4\'
            )
        ),
        array(
            \'name\'  => \'page2\',
            \'title\' => \'Page 2\',
            \'child\' => array(
                \'page2-1\' => \'Page 2.1\',
                \'page2-2\' => \'Page 2.2\',
                \'page2-3\' => \'Page 2.3\'
            )
        )
    );

    $template = array(
        \'post_type\'   => \'page\',
        \'post_status\' => \'publish\',
        \'post_author\' => 1
    );

    foreach( $pages as $page ) {
        $exists = get_page_by_title( $page[\'title\'] );
        $my_page = array(
            \'post_name\'  => $page[\'name\'],
            \'post_title\' => $page[\'title\']
        );
        $my_page = array_merge( $my_page, $template );

        $id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );

        if( isset( $page[\'child\'] ) ) {
            foreach( $page[\'child\'] as $key => $value ) {
                $child_id = get_page_by_title( $value );
                $child_page = array(
                    \'post_name\'   => $key,
                    \'post_title\'  => $value,
                    \'post_parent\' => $id
                );
                $child_page = array_merge( $child_page, $template );
                if( !isset( $child_id ) ) wp_insert_post( $child_page );
            }
        }
    }
}

SO网友:Tareq

正如@Soulseekah所说,你可以和post\\u家长一起做这件事。我没有使用以下代码进行测试,但它应该可以工作

$pages = array( 
    array(
        \'name\' => \'page1\',
        \'title\' => \'Page 1\',
        \'child\' => array(
            array(
                \'name\' => \'page11\',
                \'title\' => \'Page 1.1\'
            ),
            array(
                \'name\' => \'page12\',
                \'title\' => \'Page 1.2\'
            )
        )
    ),
    array(
        \'name\' => \'page2\',
        \'title\' => \'Page 2\',
        \'child\' => array(
            array(
                \'name\' => \'page21\',
                \'title\' => \'Page 2.1\'
            ),
            array(
                \'name\' => \'page22\',
                \'title\' => \'Page 2.2\'
            )
        )
    ),
    array(
        \'name\' => \'page3\',
        \'title\' => \'Page 3\',
        \'child\' => array(
            array(
                \'name\' => \'page21\',
                \'title\' => \'Page 2.1\'
            ),
            array(
                \'name\' => \'page22\',
                \'title\' => \'Page 2.2\'
            )
        )
    ),
);

$template = array(
    \'post_type\' => \'page\',
    \'post_status\' => \'publish\',
    \'post_author\' => 1
);

foreach( $pages as $page ) {
    $exists = get_page_by_title( $page[\'name\'] );

    if( !$exists ) {
        $my_page = array(
            \'post_name\' => $page[\'name\'],
            \'post_title\' => $page[\'title\']
        );
        $my_page = array_merge( $my_page, $template );

        $id = wp_insert_post( $my_page );

        //if there is any child page, create them by {$id} as \'post_parent\'
        if( isset( $page[\'child\'] ) ) {
            foreach( $page[\'child\'] as $child ) {
                $child_page = array(
                    \'post_name\' => $child[\'name\'],
                    \'post_title\' => $child[\'title\'],
                    \'post_parent\' => $id
                );
                $child_page = array_merge( $child_page, $template );
                $id = wp_insert_post( $child_page );
            }
        }
    }
}

结束

相关推荐

Functions.php条件标记仅适用于自定义POST类型

我正在使用gpp幻灯片,它会覆盖wordpress的默认库显示。我只想在我的自定义帖子类型“listings”上使用它-我如何引用它来仅替换wordpress在“listings”页面上的默认库?我的函数中有此代码。php,我似乎无法获得自定义帖子类型的正确参考:add_action(\'wp_head\',\'add_gpp_gallery\'); function add_gpp_gallery() { if( ( is_single() || is_page() ) &