WordPress多站点上的默认子页面

时间:2013-10-21 作者:Laura

我目前正在使用此代码在Wordpress MultiSite中创建一些默认页面。。。

function default_pages( $blog_id )
{
$default_pages = array(
    \'Contact\'     => "This is your \'Contact\' page. Enter in your content here.",
    \'Information\' => "This is your \'Information\' page. Enter in your content here.",
    \'About\'       => "This is your \'About\' page. Enter in your content here.",
    \'Home\'        => "This is your \'Home\' page. Enter in your content here.",
);

switch_to_blog( $blog_id );

if ( $current_pages = get_pages() )
    $default_pages = array_diff( $default_pages, wp_list_pluck( $current_pages, \'post_title\' ) );

foreach ( $default_pages as $page_title => $page_content ) {        
    $data = array(
        \'post_title\'   => $page_title,
        \'post_content\' => $page_content,
        \'post_status\'  => \'publish\',
        \'post_type\'    => \'page\',
    );

    wp_insert_post( add_magic_quotes( $data ) );
}

restore_current_blog();
}

add_action( \'wpmu_new_blog\', \'default_pages\' );
我只是想知道是否也可以创建子页面/子页面?例如,将“信息”作为“关于”的子页。

谢谢大家!

1 个回复
SO网友:fuxia

wp_insert_post() 返回新帖子的帖子ID。您可以使用该ID设置父帖子。

实例

$about_id = wp_insert_post(
    array (
        \'post_title\'   => \'About\',
        \'post_status\'  => \'publish\',
        \'post_type\'    => \'page\',
    )
);
wp_insert_post(
    array (
        \'post_title\'   => \'Information\',
        \'post_status\'  => \'publish\',
        \'post_type\'    => \'page\',
        \'post_parent\'  => $about_id,
    )
);
在您的情况下,我将重新构造页面数据并构建嵌套数组:

$default_pages = array(
    \'Contact\'     => array(
        "content" => "This is your \'Contact\' page. Enter in your content here."
    ),
    \'About\'       => array(
        "content" => "This is your \'About\' page. Enter in your content here.",
        "children" => array(
            \'Information\' => array(
                "content" => "This is your \'Information\' page. Enter in your content here."
            )
        ),
    ),
    \'Home\'        => array(
        "content" => "This is your \'Home\' page. Enter in your content here."
    ),
);
然后,我将页面插入移动到一个单独的函数,以允许递归:

function insert_page( $title, Array $properties, $post_parent = 0 )
{
    $data = array(
        \'post_title\'   => $title,
        \'post_content\' => $properties[ "content" ],
        \'post_status\'  => \'publish\',
        \'post_type\'    => \'page\',
        \'post_parent\'  => $post_parent,
    );

    $id = wp_insert_post( add_magic_quotes( $data ) );

    if ( ! empty ( $properties[ "children" ] ) )
    {
        foreach ( $properties[ "children" ] as $child_title => $child_properties )
            insert_page( $child_title, $child_properties, $id );
    }
}
因此,您的完整代码应该如下所示:

function default_pages( $blog_id )
{
    $default_pages = array(
        \'Contact\'     => array(
            "content" => "This is your \'Contact\' page. Enter in your content here."
        ),
        \'About\'       => array(
            "content" => "This is your \'About\' page. Enter in your content here.",
            "children" => array(
                \'Information\' => array(
                    "content" => "This is your \'Information\' page. Enter in your content here."
                )
            ),
        ),
        \'Home\'        => array(
            "content" => "This is your \'Home\' page. Enter in your content here."
        ),
    );

    switch_to_blog( $blog_id );

    if ( $current_pages = get_pages() )
        $default_pages = array_diff( $default_pages, wp_list_pluck( $current_pages, \'post_title\' ) );

    foreach ( $default_pages as $page_title => $properties )
        insert_page( $page_title, $properties );

    restore_current_blog();
}

function insert_page( $title, Array $properties, $post_parent = 0 )
{
    $data = array(
        \'post_title\'   => $title,
        \'post_content\' => $properties[ "content" ],
        \'post_status\'  => \'publish\',
        \'post_type\'    => \'page\',
        \'post_parent\'  => $post_parent,
    );

    $id = wp_insert_post( add_magic_quotes( $data ) );

    if ( ! empty ( $properties[ "children" ] ) )
    {
        foreach ( $properties[ "children" ] as $child_title => $child_properties )
            insert_page( $child_title, $child_properties, $id );
    }
}

结束

相关推荐

WordPress MultiSite(MU)子目录url/slug/permarink可以更改吗?

我目前正在与WordPress Multisite(MU)、BuddyPress和Commons in a Box(CBOX)合作,围绕一个中心用户群创建一个网站,该网站允许用户组成群组,并创建与其群组相关的“群组博客”。我在这个问题上发布了关于这个确切情况的进一步详细信息here.然而,我想问一个更一般的问题,关于WordPress多站点安装的性质,除了初始站点或网络主页。我的问题是,是否可以更改默认的子目录url结构/文件夹。例如,Multisite当前的默认设置为site。com/blog/sub-