【问题标题】:Show custom field from another CPT显示来自另一个 CPT 的自定义字段
【发布时间】:2021-06-15 22:00:11
【问题描述】:

我想查询: 我在“评估”CPT 中有一个条目,其中包含使用 ACF 制作的自定义字段,其中之一是名称“客户”,其中包含客户 ID。 我想知道如何在前端显示另一个名为“客户”的 CPT 字段。 我也用 ACF 做的自定义字段名为“profile_photo”。此CPT的作者与“assessment”CPT中的ACF自定义字段“customer”相同。

我认为它会是这样的......我不知道我是否走在正确的轨道上。

function the_field_by_author( $field, $author ) {
  $user_info = get_field( "customer" ); 
  $args = [
    'post_author'    => $user_info->ID,
    'post_type'      => 'clientes', 
    'post_status'    => 'publish',
    'posts_per_page' => 1
  ];
  $my_post = get_posts( $args );
  if( $my_post ) {
    return get_field( "photo_profile", $my_post->ID );
  }
   }

add_shortcode('shortcode_the_field_by_author', 'the_field_by_author');

我是新手,不知道如何在每个“评估”条目中显示自定义字段。 非常感谢您。

【问题讨论】:

标签: wordpress


【解决方案1】:

该方法实际上与您对默认帖子类型(例如帖子)执行的操作相同。您可以使用 get_fieldthe_field 函数。

https://www.advancedcustomfields.com/resources/get_field/ https://www.advancedcustomfields.com/resources/the_field/

只需确保在函数参数中使用适当的自定义帖子 ID。

$value = get_field( "text_field", 123 );

确保 123 是自定义帖子类型中帖子的 ID。

【讨论】:

  • 感谢您的回答。关键是每个客户在 CPT“客户”中都有一个特定的条目。在“评估”的每个条目中手动输入创建的每个出版物的 id 会很麻烦。创建新用户时会自动添加 CPT“客户”的条目。每个条目都与作为出版物“作者”的用户 ID 相关联。我必须将我保留在 CPT“评估”的自定义字段中的 ID 与 CPT“客户”的“作者”的 ID 相关联。我不知道我是否解释一下自己。
  • 我认为它会是这样的......我不知道我是否走在正确的轨道上。 'function the_field_by_author($field, $author, $cpt = 'assessment') { $user_info = get_field("customer"); $args = [ 'post_author' => $user_info->ID, 'post_type' => $cpt, 'post_status' => 'publish', 'posts_per_page' => 1 ]; $my_post = get_posts($args); if( $my_post ) { return get_field( "photo_profile", $my_post->ID ); } }'
  • 您应该在查询中包含该代码以进行澄清。也许其他人有答案。
猜你喜欢
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多