Your Location is: Home > Php

get_field() returns no data (advanced custom fields) on taxonomy

From: Sofia View: 1279 Naveen DINUSHKA 

Question

i have created a custom field on taxonomy 'course-category' and i have addeed the value from admin panel , i like to get this custom field on to the course template page. And I have wrote below code in the course template php file

   $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: above print($term_id) prints out the below

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

What I wanna achieve is the following result (ie.https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/) but on the course page not the course category page.

The print_r($app_certification1); is not printing anything at this stage, I have referred to so many documentation , forums to get the above solution and i feel its so close to the correct answer. any help is much appreciated

Best answer

With the help of you'all in above comments, i did implement it successfully below is the code snippet.( here ld_course_category refers to learndash course category, i was using 'category' earlier and that was wrong)

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

And then I wrote (app_logo is the custom field added to course category)

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

And just like app_logo above I continued using the other fields..

Hope someone finds this helpful.:)