Php 如何在前端显示wordpress分类图像?

Php 如何在前端显示wordpress分类图像?,php,wordpress,Php,Wordpress,我试图在前端显示分类图像,但不知道如何使用代码。这里我正在使用一个插件分类图像[请,有人能帮我吗 下面是我的代码: <?php $terms = get_terms('category'); foreach($terms as $term): $args = array( 'orderby' => 'title', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', '

我试图在前端显示分类图像,但不知道如何使用代码。这里我正在使用一个插件分类图像[请,有人能帮我吗

下面是我的代码:

<?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\u get\u attachment\u image()
funxtion在前端显示税务图像

wp_get_attachment_image( $term->image_id, 'detail' )
注意:
wp\u get\u attachment\u image()函数中传递
image\u id

更新答案:

是的,您还可以在循环中获得category
name
description

$term->name
$term->description

如果您使用的是分类法图像插件,则使用
wp\u get\u attachment\u 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>';
}
$terms=apply_过滤器('taxonomy images get terms','');
如果(!空($terms)){
打印“
    ”; foreach((数组)$terms作为$term){ 打印“
  • “.esc_html($term->description)。”

  • ; } 打印“
”; }
如果您使用的是高级自定义字段插件,那么这将非常容易。您希望自定义代码添加图像吗?亲爱的迪普拉,我在这里使用分类图像插件,我们可以解决它吗?仅使用此代码“.wp\u get\u attachment\u image($term->image\u id,'detail'))“'对我不起作用亲爱的迪普拉,它只在下面的代码上起作用。
$terms=apply\u过滤器('taxonomy images get terms','',数组('taxonomy'=>'category',);if(!empty($terms)){print'
    ';foreach((array)$terms as$term){print'
  • '。wp\u获取附件图像('term->image\id,'detail'))“
  • ”;}打印“
”;}
分享插件链接亲爱的迪普拉,我们能找到解决方案吗?太好了!!非常感谢亲爱的迪普拉,它可以工作了…但是我们能把它做成一个循环吗?我需要在每个类别上以图标的形式显示分类图像以及名称和描述。我们能吗?@Devbuddy如果你注意到在上面的代码中我也显示了名称和描述。亲爱的Gufran,在我的例子中,我试图在每个循环中沿着分类图像显示名称和描述。我们可以用其他方法解决它吗?@Devbuddy,你也可以在循环中获得category
name
description
$term->name
和“$term->description”`