将博客 Post 作者 link 替换为团队成员 link (CPT + ACF)

Replace Blog Post Author link with Team Member link (CPT + ACF)

我正在构建一个包含“团队成员”部分和“博客”部分的网站。团队成员是自定义 post 类型。

客户要求博客 posts 中作者的 links 应该指向关联的团队成员页面而不是 WordPress 中的默认作者页面(基本上他们不希望有默认作者页面)。

我找到了@Damocles 提供的这个解决方案 - Use "Team Members" custom post type instead of Author for blog posts

基本上,他提出的解决方案很简单,并且完全符合我最初对解决此问题的想法:

  1. 创建一个“Post 对象”ACF 字段并将其设置为通过“团队成员”自定义 post 类型
  2. 进行过滤
  3. 将此字段附加到用户帐户
  4. 转到用户个人资料并从下拉菜单中选择正确的团队成员
  5. 然后在 functions.php 中使用过滤器自动将作者 link 替换为关联的团队成员 url

有道理,但不幸的是,它不想在我的网站上工作。我什至为 ACF 字段使用了与他相同的名称,并在 functions.php:

中使用了完全相同的代码
add_filter( 'author_link', 'team_author_link', 10, 3 );
function team_author_link( $link, $author_id, $author_nicename ) {

  $team_post_id = get_field('team_post', $author_id);
  // if the team post is set, get the permalink to the team post:
  $team_link = get_permalink($team_post_id);
  $link = ($team_link !== false) ? $team_link : $link;
  return $link;
}

作者 link 确实改变了,但是所有作者 link 都指向当前打开的博客 post URL,而不是指向相关的团队成员页面.我不知道,也许我的主题覆盖了查询或其他东西,所以无法从博客 post 视图中实现 URL 到自定义 post 类型?

有人可以帮我实现吗?我想将团队成员(自定义 post 类型)附加到 WordPress 中的用户帐户,并将作者 link 到 functions.php 替换到相关的团队成员页面 url.

这是因为您缺少前缀,告诉 ACF 它应该查找用户.. - 试试这个

$team_post_id = get_field('team_post', 'user_'.$author_id);

文档在这里:https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/