Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

target a WooCommerce category's advanced custom field

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);
like image 629
Master Deo Avatar asked Dec 21 '25 01:12

Master Deo


1 Answers

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;
}
like image 129
helgatheviking Avatar answered Dec 22 '25 15:12

helgatheviking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!