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

图片转代码服务由CSDN问答提供 功能建议

我设法列出了自定义父分类的所有直接子项。 \ n

  Chocolates 
 Marshmallows 
 Popcorn 
 ..依此类推...... 
   
 
 

以下代码。

 &lt;?php 
 $ term = get_term_by('slug',get_query_var('term'),get_query_var('taxonomy')); 
if($ term-&gt; parent =  = 0){
 $ terms = get_terms('product-type','parent ='。$ term-&gt; term_id);  } 
else {$ terms = get_terms('product-type','parent ='。$ term-&gt; parent);  } 
foreach($ terms as $ term){
echo'
&lt; div class =“snack_type”&gt; 
&lt; a href =“'。get_term_link($ term)。'”&gt;'  。  $ term-&gt;名称。  '&LT; / A&GT; 
&LT; / DIV&GT; 
 
';  } 
?&gt; 
   
 
 

上面显示的每个分类法都会使用自定义字段(高级自定义字段)上传到其中的图像。 如何在生成的每个div中显示自定义字段(product_type_image)? 喜欢这个

  Chocolates [product_type_image] 
 Marshmallows [product_type_image] 
 Popcorn [product_type_image] 
 ..依此类推...... 
   
 
 

我正在尝试

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

但尝试显示任何内容都失败

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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) . '"/>';
    }
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题