get_field() 在我的 foreach 循环中不起作用
get_field() does not work inside my foreach loop
我为我的产品类别创建了一个自定义字段,这样我就可以 select 使用 ACF 插件相应地使用图标。我这样称呼所有类别(但是自定义字段的值 return 什么都没有,因为我已经尝试过 var_dumping):
<?php
$orderby = 'date';
$order = 'DESC';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
foreach ($product_categories as $key => $category) {
$icon = get_field('icon_classname');
if (empty($icon)) { $icon = 'flaticon-002-coat'; }
?>
<div class="cat-item">
<div class="cat cat-1">
<div class="cat-wrap">
<a class="category" href="<?php echo get_term_link($category); ?>">
<i class="icon fimanager <?php echo $icon; ?>"></i>
<span class="cat-title"><?php echo $category->name; ?></span>
</a>
</div>
</div>
</div>
<?php
} wp_reset_postdata();
}
?>
请检查:
你应该替换这两行:
foreach( $product_categories as $cat) {
$icon = get_field('icon_classname', 'category_'.$cat->term_id);
我为我的产品类别创建了一个自定义字段,这样我就可以 select 使用 ACF 插件相应地使用图标。我这样称呼所有类别(但是自定义字段的值 return 什么都没有,因为我已经尝试过 var_dumping):
<?php
$orderby = 'date';
$order = 'DESC';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
foreach ($product_categories as $key => $category) {
$icon = get_field('icon_classname');
if (empty($icon)) { $icon = 'flaticon-002-coat'; }
?>
<div class="cat-item">
<div class="cat cat-1">
<div class="cat-wrap">
<a class="category" href="<?php echo get_term_link($category); ?>">
<i class="icon fimanager <?php echo $icon; ?>"></i>
<span class="cat-title"><?php echo $category->name; ?></span>
</a>
</div>
</div>
</div>
<?php
} wp_reset_postdata();
}
?>
请检查:
你应该替换这两行:
foreach( $product_categories as $cat) {
$icon = get_field('icon_classname', 'category_'.$cat->term_id);