2
votes

I'm trying to get the advanced custom field for a WooCommerce category. With the following code I get the woocommerce categories:

$categories = get_terms('product_cat');
  var_dump($categories);

But why isn't any ACF info included? Is there another function which do gets the ACF info?

UPDATE

This is outside the loop. So I'm trying to get a custom field of a specific product category. I found this info: http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

I can't get it to work.

Answer

To get the ACF with get_field(). I needed to use the cat_ID of the array I got with get_categories(). (Maybe it also works with get_terms())

I failed to grasp te second parameter in get_field() I made it in the following way:

$id = 'product_cat_' . $category->cat_ID;
echo get_field ('field_name', $id);
2
See get_terms(). Meta is never returned. Please rephrase your question with what you are specifically trying to do.helgatheviking
I've got it to work. Thx helgatheviking for pointing me in te right direction.Master Deo

2 Answers

1
votes

Based on the documentation of ACF it appears you would get the custom term data as follows:

$category = get_term_by('slug', 'your-category', 'product_cat');
If( ! is_wp_error( $category ) && $custom_field = get_field('your_custom_field', $category ) ){
   echo $custom_field;
}
1
votes
// get the current taxonomy term
$term = get_queried_object();

// vars
$image = get_field('image', $term);
$color = get_field('color', $term);

see documentation here