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

编程入门 行业动态 更新时间:2024-10-19 09:36:19
本文介绍了在自定义分类模板中获取自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

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

本文发布于:2023-11-28 16:44:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字段   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!