自动为每个用户创建帖子并为其分配模板

时间:2015-02-18 22:15:51

标签: php wordpress wordpress-theming custom-wordpress-pages

我试图让wordpress为每个用户创建新的页面或帖子并将给定的模板指向它。它按预期创建帖子/页面,但不会将任何模板附加到其上。关于这可以如何帮助的任何想法。我的代码如下所示。

function my_create_page($user_id){
    $the_user = get_userdata($user_id);
    $new_user_name = $the_user->user_login;
    $my_post = array();
    $my_post['post_title'] = $new_user_name;
    $my_post['post_type'] = 'page';
    $my_post['post_content'] = '';
    $my_post['post_status'] = 'publish';
    $my_post['post_theme'] = 'user-profile';
    wp_insert_post( $my_post );
}
add_action('user_register', 'my_create_page', 'user-profile.php');

1 个答案:

答案 0 :(得分:0)

你只是错过了一行:

function my_create_page($user_id){
    $the_user = get_userdata($user_id);
    $new_user_name = $the_user->user_login;
    $my_post = array();
    $my_post['post_title'] = $new_user_name;
    $my_post['post_type'] = 'page';
    $my_post['post_content'] = '';
    $my_post['post_status'] = 'publish';
    $my_post['post_theme'] = 'user-profile';

    $my_post['page_template'] = 'name-of-template.php';

 wp_insert_post( $my_post );
}
add_action('user_register', 'my_create_page', 'user-profile.php'); 

Codex: wp_insert_post