【发布时间】:2012-10-21 14:03:30
【问题描述】:
我正在寻找一些帮助,以查找将在自定义 WooCommerce 主题中显示产品类别图像的 php sn-p。我正在使用一个在小部件中执行 php 代码的插件,它适用于产品类别名称。我只是找不到任何适用于类别图像的东西。任何帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce
我正在寻找一些帮助,以查找将在自定义 WooCommerce 主题中显示产品类别图像的 php sn-p。我正在使用一个在小部件中执行 php 代码的插件,它适用于产品类别名称。我只是找不到任何适用于类别图像的东西。任何帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce
假设您知道类别 ID,并且它位于 $cat_ID:
// get the thumbnail ID
$thumbnail_id = get_woocommerce_term_meta( $cat_ID, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo '<img src="'.$image.'" />';
【讨论】:
<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1> 正确填充的标题
$term->term_id获取类别ID:ericwijaya.wordpress.com/2012/02/16/…
要在archive-product.php中显示当前显示类别的类别图像,当is_product_category()为真时使用当前类别term_id:
// verify that this is a product category page
if (is_product_category()){
global $wp_query;
// get the query object
$cat = $wp_query->get_queried_object();
// get the thumbnail id user the term_id
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo '<img src="'.$image.'" alt="" width="762" height="365" />';
}
【讨论】:
参考以下代码:-
global $product;
if (is_product_category()) {
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id);
if ($image) {
echo '<img src="' . esc_url($image) . '" alt="" />';
} }
【讨论】: