想要在帖子列表中显示类别图标,为此,我正在使用ACF创建自定义字段category_icon字段。 下面是我获取图标图像URL的代码,但是什么也没有得到。
<span class="blog-info-categories">
<?php
print apply_filters( 'taxonomy-images-queried-term-image', '' );
$terms = get_terms('category');?>
<?php
$taxonomy = 'category';
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));
// Separator between links.
$separator = ', ';
if (!empty($post_terms) && !is_wp_error($post_terms)) {
$term_ids = implode(',', $post_terms);
$terms = wp_list_categories(array(
'title_li' => '',
'style' => 'none',
'echo' => false,
'taxonomy' => $taxonomy,
'include' => $term_ids));
$terms = rtrim(trim(str_replace('<br />', $separator, $terms)), $separator); $termss = wp_get_post_terms( $post->ID,array( 'category' ) );
$icon = get_field('category_icon', $taxonomy . '_' . $term_ids);
echo $icon['url'];
echo $terms;
}
?>
</span>
答案 0 :(得分:0)
wp_list_categories()函数输出一个html列表,该列表是我将始终使用get_terms()的字符串,因此我需要循环进行一些操作。这是您的问题的解决方案吗?我想我理解,但是如果我错了,可以修改解决方案以适应问题。
$terms = get_terms( $taxonomy, array(
'hide_empty' => false,
'include' => $post_terms
)
);
foreach($terms as $term){
$icon = get_field('category_icon', $term);
$icon = $icon['url'];
$name = $term->name;
$url = get_term_link($term, $taxonomy);
echo '<img src="' . $icon . '">' . '<a href="' . $url . '">' . $name . '</a>, ';
}