如何显示Woocommerce Category图像?

时间:2012-10-03 21:21:24

标签: php wordpress woocommerce thumbnails

我在PHP中使用此代码:

$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

有什么建议吗?

10 个答案:

答案 0 :(得分:40)

要在archive-product.php中显示当前显示的类别的类别图像,请在term_id为真时使用当前类别is_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' />";
}

答案 1 :(得分:3)

你也可以使用foreach循环来显示父类别给出的父类别的显示类别图像等。

例如,我给了74个父类别的id,然后我将显示来自子类别及其slug的图像。

**<?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; ?>** 

答案 2 :(得分:3)

要防止全尺寸类别图片减慢页面速度,您可以使用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>';

?>

编辑:修正变量名称和缺少引号

答案 3 :(得分:2)

来自WooCommerce page

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

答案 4 :(得分:1)

/wp-content/plugins/woocommerce/templates/循环路径中添加代码

    <?php
        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 ); 
            echo "<img src='{$image}' alt='' />";
        }
    ?>

答案 5 :(得分:1)

从Woo 3.6.0开始,

get_woocommerce_term_meta被废弃。

所以改变

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

进入:($ value-> term_id应为woo类别ID)

get_term_meta($value->term_id, 'thumbnail_id', true)

有关详细信息,请参阅文档: https://docs.woocommerce.com/wc-apidocs/function-get_woocommerce_term_meta.html

答案 6 :(得分:0)

使用此代码,这可能会对你有所帮助。我已经通过了cat id 17.pass woocommerce cat 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">';
   ?>

答案 7 :(得分:0)

<?php 

	$terms = get_terms( array(
	'taxonomy' => 'product_cat',
	'hide_empty' => false,
	) ); // Get Terms

foreach ($terms as $key => $value) 
{
	$metaterms = get_term_meta($value->term_id);
	$thumbnail_id = get_woocommerce_term_meta($value->term_id, 'thumbnail_id', true );
	$image = wp_get_attachment_url( $thumbnail_id );
	echo '<img src="'.$image.'" alt="" />';
} // Get Images from woocommerce term meta

?>

答案 8 :(得分:0)

此解决方案只需很少的代码。我认为更好。

<?php echo wp_get_attachment_image( get_term_meta( get_queried_object_id(), 'thumbnail_id', 1 ), 'thumbnail' ); ?>

答案 9 :(得分:0)

原始答案有所帮助,但已过时。

来自https://docs.woocommerce.com/document/woocommerce-display-category-image-on-category-archive/

 /**
 * 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_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        if ( $image ) {
            echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
        }
    }
}

我添加了一个类 echo '<img src="' . $image . '" alt="' . $cat->name . '" class="catImage" />';,然后使用

.catImage{
  float: left;
  max-height: 100px;
  padding-right: 10px;
}