2012-10-03 29 views
9

我在PHP中使用此代碼:如何顯示Woocommerce分類圖像?

$idcat = 147; 
$thumbnail_id = get_woocommerce_term_meta($idcat, 'thumbnail_id', true); 
$image = wp_get_attachment_url($thumbnail_id); 
echo '<img src="'.$image.'" alt="" width="762" height="365" />'; 

147在當前ID手動設置的,但我需要電流id在其他類別

任何建議?

+1

到底是什麼問題?您需要檢索當前類別ID的類別圖像而不明確設置它? – doublesharp

+0

對不起,我需要顯示當前woocommerce產品類別的圖片。 – MrRoman

回答

33

archive-product.php當前顯示的類別顯示類別圖像,使用當前類別term_idis_product_category()是正確的:

// 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 using the queried category 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

謝謝你的回答,但不能正常工作,因爲woocommerce模板中的「is_category」是「is_product_category」和「get_query_var('cat')」 - >「貓」? 有沒有建議? 我使用woocommerce模板來覆蓋默認的woocommerce。並即時編輯product-archive.php模板。 – MrRoman

+0

你的意思是archive-product.php?這是用於商店主頁以及標籤檔案的模板,例如'/ product-tag/TAG-NAME'。如果是這樣的話,我假設你正試圖獲得循環中每個產品的類別圖像?或者您正在查看單個產品類別並需要顯示該類別的圖像,例如'/ product-category/CATEGORY'? – doublesharp

+0

是的/產品類別/ CATEGORY,當我創建一個新類別時,我可以上傳woocommerce管理員的圖片。 P. D .:用西班牙語(我的母語:))是/ categoria-producto/categoria – MrRoman

2

您也可以用於顯示類別圖像和foreach循環等從父類給出由父母身份證。

例如,我給父母類別74 id,然後我會顯示圖像從子類別和它的slu also也。

**<?php 
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'child_of'=>'74')); 
foreach($catTerms as $catTerm) : ?> 
<?php $thumbnail_id = get_woocommerce_term_meta($catTerm->term_id, 'thumbnail_id', true); 

// get the image URL 
$image = wp_get_attachment_url($thumbnail_id); ?> 
<li><img src="<?php echo $image; ?>" width="152" height="245"/><span><?php echo $catTerm->name; ?></span></li> 
<?php endforeach; ?>** 
0

使用此代碼,這可能會幫助你。我已經通過了貓ID 17.pass woocommerce貓ID和多數民衆贊成它

<?php 
     global $woocommerce; 
     global $wp_query; 
     $cat_id=17; 
     $table_name = $wpdb->prefix . "woocommerce_termmeta"; 
     $query="SELECT meta_value FROM {$table_name} WHERE `meta_key`='thumbnail_id' and woocommerce_term_id ={$cat_id} LIMIT 0 , 30"; 
     $result = $wpdb->get_results($query); 

     foreach($result as $result1){ 
      $img_id= $result1->meta_value; 
     }  

     echo '<img src="'.wp_get_attachment_url($img_id).'" alt="category image">'; 
    ?> 
1

從WooCommerce頁:

// WooCommerce – display category image on category archive 

add_action('woocommerce_archive_description', 'woocommerce_category_image', 2); 
function woocommerce_category_image() { 
    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="' . $image . '" alt="" />'; 
     } 
    } 
} 
0

要顯示Woocommerce分類圖片

使用此代碼 -

add_action('woocommerce_archive_description', 'woocommerce_category_image', 20); 
function woocommerce_category_image() 
{ 
    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="" />'; 
     } 
    } 
    } 
2

爲了防止全尺寸圖像類網頁放緩下來,你可以用較小的圖像與wp_get_attachment_image_src()

<?php 

$thumbnail_id = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true); 

// get the medium-sized image url 
$image = wp_get_attachment_image_src($thumbnail_id, 'medium'); 

// Output in img tag 
echo '<img src="' . $image[0] . '" alt="" />'; 

// Or as a background for a div 
echo '<div class="image" style="background-image: url("' . $image[0] .'")"></div>'; 

?> 

編輯:固定變量名和失蹤報價