【问题标题】:Get custom fields in custom taxonomy template在自定义分类模板中获取自定义字段
【发布时间】:2015-10-15 21:53:32
【问题描述】:

我已经设法列出了自定义父分类的所有直系子级..

 Chocolates
 Marshmallows  
 Popcorn
 ..and so on...

下面的代码。

<?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>

'; }
?>

上面显示的每个分类都有一张带有自定义字段(高级自定义字段)的图片上传到其中。如何在生成的每个 div 中显示自定义字段(product_type_image)?像这样

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

我正在尝试

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

但尝试显示任何内容均未成功

【问题讨论】:

    标签: php wordpress custom-fields advanced-custom-fields custom-taxonomy


    【解决方案1】:

    添加这一行

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

    应该是这样的

    <?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) . '"/>';
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多