将其创建为插件并激活。它为作者创建主页,并为每个作者+或更好的角色创建子页后,将停用自身。最后,它会自行停用。
如你所愿进行改进。它使用
// Hook activation to create new Author Pages
register_activation_hook(__FILE__, function(){
// Create a Parent Page for all Author Pages
if(!($parent = get_page_by_title(\'Authors\'))){
$parent = wp_insert_post(array(
\'post_type\' => \'page\',
\'post_title\' => \'Authors\',
\'post_content\' => \'Authors are children of this page.\',
\'post_status\' => \'draft\', // Or publish
));
}
if(!$parent){
// Bad... ERROR!
return;
}
// Get user IDs, get_users() returns too much data
global $wpdb;
$IDs = $wpdb->get_col("SELECT `ID` FROM {$wpdb->users} ORDER BY `user_registered` DESC;");
// Loop IDs and create subpages for Authors+ (not Subscribers)
foreach($IDs as $ID){
// Get user
$user = new WP_User($ID);
// Only create pages for Authors!
if(!$user->has_cap(\'edit_posts\')) continue;
// Create page for Author
$title = "About Author: {$user->display_name}";
if(!($child = get_page_by_title($title))){
$child = wp_insert_post(array(
\'post_type\' => \'page\',
\'post_title\' => $title,
\'post_name\' => $user->display_name,
\'post_content\' => \'Write stuff about the Author.\',
\'post_status\' => \'draft\', // Or publish
\'post_parent\' => $parent,
));
// Setup according Metas (for further tracking)
update_post_meta($child, \'about_author\', $user->user_login);
update_post_meta($child, \'about_author_ID\', $user->ID);
}
}
// Done! WILL RUN JUST ONCE, deactivates itself afterwards.
deactivate_plugins(__FILE__, true);
die;
});
这是一种黑客方法,但可以满足您的需要它使用PHP 5.3闭包。考虑恢复到PHP 5.2兼容性分配:)
Regards.