如何将自定义 post 标题显示为 "first_name last_name"

How do I display custom post title as "first_name last_name"

我创建了一个网站,运动员可以在该网站上注册并创建自己的模板化个人资料页面。注册时,我会自动创建一个自定义 post(他们的个人资料)并将用户设置为 post.

的作者

但是,我终生无法将 post 标题设置为用户(现在是作者)的名字和姓氏。例如,如果 John Smith 注册,我希望 post 标题显示为 John Smith,并将 slug 转换为 athlete/john.smith.

我现在使用 $user_info->nickname,但这会导致标题和别名都显示为 john.smith

这是我正在使用的代码 - 任何指针将不胜感激:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $user_info->nickname,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}

您可以像下面这样使用名字和姓氏:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    $first_name = $user_info->first_name;
    $last_name = $user_info->last_name;

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $first_name . ' ' . $last_name,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}

未测试,但应该可以。

您还应该使用 post_name 作为 slug(john.smith) 和 first_name,post 标题的姓氏,如下所示:

add_action( 'user_register', 'wpse_216921_company_cpt', 20, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {
        
        $post_title = $user_info->first_name.' '.$user_info->last_name;
        $post_title = trim(ucwords($post_title));
        $post_slug  = preg_replace('/\s+/', '.', $post_title);

        // Create a new post
        $user_post = array(
            'post_title'   => $post_title, //$user_info->display_name,
            'post_name'    => $post_slug,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}

感谢大家的帮助。原来是Ultimate Member的问题,需要用到UM Hook um_registration_complete.

最终的解决方案如下:

//Create Custom Post Type on registration

add_action( 'um_registration_complete', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
   // Get user info
   $user_info = get_userdata( $user_id );
   $user_roles = $user_info->roles;

   // New code added
   $this_user_role = implode(', ', $user_roles );

   if ($this_user_role == 'author') {

   $post_title = $user_info->first_name.' '.$user_info->last_name;
   $post_title = trim(ucwords($post_title));
   $post_slug = preg_replace('/\s+/', '.', $post_title);

   // Create a new post
   $user_post = array(
   'post_title' => $post_title, //$user_info->display_name,
   'post_name' => $post_slug,
   'post_status' => 'publish', // <- here is to publish
   'post_type' => 'athlete', // <- change to your cpt
   'post_author' => $user_info->ID
   );
   // Insert the post into the database
   $post_id = wp_insert_post( $user_post );
   }
}