2012-11-01 36 views
0

我正在寻找一些帮助找到一个php代码片段,它将在自定义woocommerce主题中显示产品类别图像。我正在使用一个插件来执行一个小部件中的php代码,并且它对产品类别名称工作正常。我无法找到适用于类别图片的任何内容。任何帮助,将不胜感激。 感谢 TC定制woocommerce主题中的产品类别图像

回答

0

假设你知道类别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.'" />'; 
+0

感谢您的回复。我对此很新。你知道它会检测类别的方式吗?我宁愿为所有类别页面提供一个侧栏。标题使用'

<?php the_title(); ?>

' – user1790067

+0

这样做,那么你可以使用'$ term-> term_id'来获得类别ID:http://ericwijaya.wordpress.com/2012/02/16/display-product-category-description -in-产品页面上woocommerce / – doublesharp

0

要显示在归档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" />'; 
    } 
0

参见下面的代码: -

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="" />'; 

    } } 
相关问题