dongxian2863 2015-07-25 06:53
浏览 183
已采纳

在自定义分类模板中获取自定义字段

I've managed to list out all direct children of the custom parent taxonomy..

 Chocolates
 Marshmallows  
 Popcorn
 ..and so on...

with this code below.

<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) { 
$terms = get_terms( 'product-type', 'parent='.$term->term_id ); } 
else { $terms = get_terms( 'product-type', 'parent='.$term->parent ); } 
foreach($terms as $term) { 
echo '
<div class="snack_type">
<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>
</div>

'; }
?>

Each of the taxonomies showing above has an image uploaded to it with custom fields (advanced custom fields). How do i show a custom field (product_type_image) in each of the div that gets generated? like this

 Chocolates [product_type_image]
 Marshmallows [product_type_image] 
 Popcorn [product_type_image]
 ..and so on...

i was trying out with

 $productimage = get_field('product_type_image', $term->taxonomy.'_'.$term->term_id);

but it was unsuccessful to try to show anything

  • 写回答

1条回答 默认 最新

  • dongmou1964 2015-07-25 07:39
    关注

    add this line

    echo '<img src="' . get_field('product_type_image', $term->taxonomy . '_' . $term->term_id) . '"/>';
    

    so it should look like this

    <?php 
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    if ($term->parent == 0) { 
    $terms = get_terms( 'product-type', 'parent='.$term->term_id ); } 
    else { $terms = get_terms( 'product-type', 'parent='.$term->parent ); } 
    foreach($terms as $term) { 
    echo '<div class="snack_type"><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></div>'; 
    echo '<img src="' . get_field('product_type_img', $term->taxonomy . '_' . $term->term_id) . '"/>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功