【问题标题】:How do I display custom post title as "first_name last_name"如何将自定义帖子标题显示为“first_name last_name”
【发布时间】:2021-11-28 08:21:24
【问题描述】:

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

但是,我不能将帖子标题设置为用户(现在是作者)的名字和姓氏。例如,如果 John Smith 注册了,我希望帖子标题为 John Smith,并将 slug 转换为 athlete/john.smith.

我现在使用$user_info->nickname,但这会导致标题和 slug 都读为 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 );
    }
}

【问题讨论】:

  • 你也应该使用 post_name 作为 slug 和 first_name,last name 作为帖子标题

标签: php wordpress advanced-custom-fields custom-post-type ultimate-member


【解决方案1】:

您还应该使用 post_name 作为 slug(john.smith) 和 first_name,last name 作为帖子标题,如下所示:

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 );
    }
}

【讨论】:

  • 谢谢拉吉夫!不幸的是,我仍然无法将其显示为 [first_name.last_name] 的标题,因此我无法测试您的 post_slug sn-p 是否有效(它看起来不错,所以我确定它确实有效)。问题可能是代码在实际元数据保存在我的数据库中之前触发,因此没有从键中获取 first_name 和 last_name 元数据?我正在使用 Ultimate Member 注册表单插件,但数据肯定会在创建用户时发送到我的数据库。
  • 嗨,如果你想要一个像 [first_name.last_name] 这样的标题,那么你应该将 $post_title 更改/设置/替换为 $post_slug,因为当前你的帖子标题($post_title)是 [first_name last_name] 并发布slug($post_slug) 像 [first_name.last_name],在 $user_post array() 上用 $post_slug 替换 $post_title 像这样: $user_post = array( 'post_title' => $post_slug , 'post_name' => $post_title ) 并尝试这个
【解决方案2】:

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

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 );
    }
}

未经测试,但应该可以。

【讨论】:

  • 非常感谢@akhtarujjaman 的快速回复!这就是我最初认为可行的方法,但是当我使用该代码时,帖子标题一直被设置为“无标题”。我检查了数据库,“first_name”和“last_name”元值在那里,但由于某种原因,它们不用于填充标题。
【解决方案3】:

感谢各位的帮助。原来是 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 );
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2012-02-21
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多