WordPress的。如何在类别中获取自定义文件?

时间:2016-09-06 10:17:54

标签: wordpress

我在我的wp网站上安装了高级自定义字段类别。但我无法在循环类别中获得字段。代码:

$args = array(
    'type'         => 'post',
    'orderby'      => 'name',
    'order'        => 'ASC',
    'hide_empty'   => 0,
    'taxonomy'     => 'catalog'
);

    $categories = get_categories( $args );
    if( $categories ){
        foreach( $categories as $cat ){
             echo get_term_meta($cat->cat_ID, 'image_cat',true); //empty
        }
    }

我如何从ACF获得字段?

2 个答案:

答案 0 :(得分:1)

$categories = get_categories( $args );
if( $categories ){
  foreach( $categories as $cat ){
    the_field('image_cat', $cat);
  }
}

在ACF中正常,函数中的第一个值是自定义字段名称,第二个值是类别对象

答案 1 :(得分:0)

$categories = get_categories( $args );
  if( $categories ){
      foreach( $categories as $cat ){
        $taxonomy = $cat->taxonomy;
        $term_id = $cat->term_id;  

        // load thumbnail for this taxonomy term (term object)
        $thumbnail = get_field('image_cat', $cat);

       // load thumbnail for this taxonomy term (term string)
       $thumbnail = get_field('image_cat', $taxonomy . '_' . $term_id);
    }
 }