在自定义分类模板中获取自定义字段 [英] Get custom fields in custom taxonomy template

查看:72
本文介绍了在自定义分类模板中获取自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法列出了自定义父分类法的所有直接子代.

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

 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)?像这样

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

我正在尝试

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

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

but it was unsuccessful to try to show anything

推荐答案

添加此行

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

这篇关于在自定义分类模板中获取自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆