【发布时间】:2018-07-18 09:51:52
【问题描述】:
我已使用以下代码获取 Woocommerce 产品类别的缩略图 URL,但它仅输出带有 src="unknown" 的 <img> 标签。
$cat_slug = t-shirts;
$thumbnail_id = get_woocommerce_term_meta( $cat_slug, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="50" height="50" />';
让它发挥作用的最佳方法是什么?
编辑
在第二次调用牛仔裤类别的缩略图时,它只输出<img src(unknown) alt="" width="50" height="50" />。
<div class="list-item">
<div class="item-img">
<?php
$term_slug = 't-shirts';
$taxonomy = "product_cat";
$term_id = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
$thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="50" height="50" />';
?>
</div>
<a href="#">
<div class="item-name">
<?php if( $term = get_term_by('slug', 't-shirts', 'product_cat') ) echo $term->name;?>
</div>
</a>
</div>
<div class="list-item">
<div class="item-img">
<?php
$term_slug = 'jeans';
$taxonomy = "product_cat";
$term_id = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
$thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="50" height="50" />';
?>
</div>
<a href="#">
<div class="item-name">
<?php if( $term = get_term_by('slug', 'jeans', 'product_cat') ) echo $term->name;?>
</div>
</a>
</div>
【问题讨论】:
-
只是带有 src="unknown" 的 img 标签。我已经编辑了这个问题。谢谢
-
$cat_slug = 't-shirts';。进行此更改 -
同样的结果。它输出
<img src=(unknown) alt="" width="50" height="50"> without the image url.
标签: php wordpress image woocommerce custom-taxonomy