首页 > 解决方案 > get_field() 不返回分类数据(高级自定义字段)

问题描述

我在分类“课程类别”上创建了一个自定义字段,并添加了管理面板中的值,我喜欢将此自定义字段添加到课程模板页面。我在课程模板php文件中写了下面的代码

   $categories= get_the_category();
      print_r($categories);
      if (!empty($categories)) {
        $term_id = $categories[0]->term_id;
        echo "ok";
        print_r($term_id);
        $app_certification1 = get_field('app_certification','term_'.$term_id);
  print_r($app_certification1);

PS:上面的 print($term_id) 打印出下面的

Array ( [0] => WP_Term Object ( [term_id] => 315 [name] => Accountants [slug] => accountants [term_group] => 0 [term_taxonomy_id] => 315 [taxonomy] => category [description] => [parent] => 0 [count] => 8 [filter] => raw [cat_ID] => 315 [category_count] => 8 [category_description] => [cat_name] => Accountants [category_nicename] => accountants [category_parent] => 0 ) [1] => WP_Term Object ( [term_id] => 281 [name] => Book Keepers [slug] => book-keepers [term_group] => 0 [term_taxonomy_id] => 281 [taxonomy] => category [description] => [parent] => 0 [count] => 8 [filter] => raw [cat_ID] => 281 [category_count] => 8 [category_description] => [cat_name] => Book Keepers [category_nicename] => book-keepers [category_parent] => 0 ) [2] => WP_Term Object ( [term_id] => 247 [name] => Xero Practice Manager [slug] => xpm [term_group] => 0 [term_taxonomy_id] => 247 [taxonomy] => category [description] => [parent] => 0 [count] => 22 [filter] => raw [cat_ID] => 247 [category_count] => 22 [category_description] => [cat_name] => Bero C anager [category_nicename] => xpb [category_parent] => 0 ) )

我想要实现的是以下结果(即 https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/),但在课程页面而不是课程类别页面上。

在这个print_r($app_certification1);阶段没有打印任何东西,我参考了很多文档,论坛来获得上述解决方案,我觉得它非常接近正确答案。任何帮助深表感谢

标签: phphtmlwordpressadvanced-custom-fieldscustom-wordpress-pages

解决方案


在以上评论中大家的帮助下,我确实成功实现了它,下面是代码片段。(这里 ld_course_category 指的是 learndash 课程类别,我之前使用的是“类别”,这是错误的)

$gotTerms=get_the_terms(  $post->ID , 'ld_course_category' );

然后我写了(app_logo 是添加到课程类别的自定义字段)

  $applogo    =  get_field('app_logo', $gotTerms[0]);
             echo $applogo;

就像上面的 app_logo 一样,我继续使用其他字段..

希望有人觉得这有帮助。:)


推荐阅读