【问题标题】:target a WooCommerce category's advanced custom field定位 WooCommerce 类别的高级自定义字段
【发布时间】:2015-12-30 08:43:15
【问题描述】:

我正在尝试获取 WooCommerce 类别的高级自定义字段。使用以下代码,我得到了 woocommerce 类别:

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

但为什么不包含任何 ACF 信息?是否有另一个函数可以获取 ACF 信息?

更新

这是在循环之外。所以我正在尝试获取特定产品类别的自定义字段。我找到了这个信息:http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

我无法让它工作。

回答

使用 get_field() 获取 ACF。我需要使用通过 get_categories() 获得的数组的 cat_ID。 (也许它也适用于 get_terms())

我没有掌握 get_field() 中的第二个参数 我是通过以下方式制作的:

$id = 'product_cat_' . $category->cat_ID;
echo get_field ('field_name', $id);

【问题讨论】:

  • get_terms()。 Meta 永远不会返回。请用您的具体目标重新表述您的问题。
  • 我已经开始工作了。感谢 helgatheviking 为我指明了正确的方向。

标签: php woocommerce advanced-custom-fields


【解决方案1】:

根据documentation of ACF,您似乎将获得如下自定义术语数据:

$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;
}

【讨论】:

    【解决方案2】:
    // get the current taxonomy term
    $term = get_queried_object();
    
    // vars
    $image = get_field('image', $term);
    $color = get_field('color', $term);
    

    参见文档here

    【讨论】:

    • 最佳答案
    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2015-07-31
    • 2017-12-16
    相关资源
    最近更新 更多