[英]How can i display the wordpress taxonomy image into front-end?
我试图将分类图像显示在前端,但是我不知道如何使用代码。 我在这里使用的插件Taxonomy Images [ https://wordpress.org/plugins/taxonomy-images/]请问,有人可以帮我吗?
这是我的代码如下:
<?php $terms = get_terms('category');
foreach($terms as $term):
$args = array(
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($term->slug)
)
));
$tag_query = new WP_Query( $args );
if($tag_query->have_posts()):
$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '' );
echo '<li>
' . wp_get_attachment_image( $term->image_id, 'detail' ) . '
<a href="#">'.esc_html($term->name).'</a>
<p>'.esc_html($term->description).'</p>
</li>';
while($tag_query->have_posts()):$tag_query->the_post();
endwhile;
endif;
wp_reset_postdata();
endforeach;
?>
使用wp_get_attachment_image()
函数可在前端显示分类图像。
wp_get_attachment_image( $term->image_id, 'detail' )
注意:在wp_get_attachment_image()
函数中传递image_id
。
更新的答案:
是的,您还可以在循环中获取类别name
和description
。
$term->name
$term->description
如果您正在使用分类法图像插件,请使用wp_get_attachment_image
方法获取图像。 以下是为您提供的完整代码。
检查下面的代码,这将帮助您。
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach ( (array) $terms as $term ) {
print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . ' <a href="#">'.esc_html($term->name).'</a>
<p>'.esc_html($term->description).'</p></li>';
}
print '</ul>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.