题
当创建新博客时,将自动创建“关于”之类的页面。同样,我还需要一些其他页面,这些页面应在我的多源下创建博客时自动出现。
如何配置要在多站点下使用新博客创建要创建的默认页面?
for ex。:如果我有一个多站点 example.com
. 。在本网站下创建的每个博客都应有家庭,我的商店和地址。
解决方案
我建议您在您的functions.php文件中创建一个与操作钩链接的函数 activate_blog
. 。使用WordPress函数 get_pages() 查看您的默认页面是否存在。如果他们不这样做,请与 wp_insert_post.
add_action('activate_blog','my_default_pages');
function my_default_pages(){
$default_pages = array('About','Home','My Store','My Address');
$existing_pages = get_pages();
foreach($existing_pages as $page){
$temp[] = $page->post_title;
}
$pages_to_create = array_diff($default_pages,$temp);
foreach($pages_to_create as $new_page_title){
// Create post object
$my_post = array();
$my_post['post_title'] = $new_page_title;
$my_post['post_content'] = 'This is my '.$new_page_title.' page.';
$my_post['post_status'] = 'publish';
$my_post['post_type'] = 'page';
// Insert the post into the database
$result = wp_insert_post( $my_post );
}
}
要在您自己的网站上测试此功能,请尝试将挂钩设置为 wp_head
. 。它将在每个页面上运行,并插入不存在的页面,其中内容为$ my_post ['post_content']。 *当博客在多站点上下文中创建博客时,'activate_blog'挂钩是否运行?我不知道。*
有关可用的默认参数的完整列表,请参阅wp_insert_post的codex页面。
其他提示
使用“ wpmu_activate_blog”而不是“ activate_blog”。它对我有用。谢谢