【问题标题】:Automatically create a post for every user and assign a template to it自动为每个用户创建帖子并为其分配模板
【发布时间】:2015-02-18 22:15:51
【问题描述】:

我正在尝试让 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');

【问题讨论】:

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


    【解决方案1】:

    你只是少了一行:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多