35

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

当前ID在哪里147手动设置,但我需要在其他类别的当前ID

有什么建议吗?

4

11 回答 11

77

要在 中显示当前显示的类别的类别图像,请在为 true时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_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' />";
}
于 2012-10-03T21:27:31.783 回答
7

get_woocommerce_term_meta 自 Woo 3.6.0 以来已被弃用。

所以改变

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

into: ($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

于 2019-06-25T13:32:21.960 回答
5

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="" />';
      }
  }
}
于 2015-11-21T12:58:30.767 回答
4

您还可以使用 foreach 循环来显示由父 ID 给出的父类别中的类别图像等。

例如,我给出父类别的 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; ?>** 
于 2013-08-21T10:52:41.680 回答
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>';

?>

编辑:固定变量名和缺少引号

于 2017-11-14T12:53:47.327 回答
2

这个解决方案的代码很少。我认为更好。

<?php echo wp_get_attachment_image( get_term_meta( get_queried_object_id(), 'thumbnail_id', 1 ), 'thumbnail' ); ?>
于 2020-08-02T04:32:50.263 回答
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='' />";
        }
    ?>
于 2018-08-03T07:26:55.257 回答
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

?>

于 2018-08-17T06:04:08.787 回答
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;
}
于 2021-07-13T00:58:01.127 回答
0

我是这样做的:

function loop_product_category_image(){
    global $wp_query;
    $terms = get_the_terms( $wp_query->ID, 'product_cat' );
    $thumbnail_id = get_term_meta( $terms[0]->term_id, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );
    echo "<img src='{$image}' alt='category image' width='162' height='165' />";
}

例如,您可以挂钩:在产品循环中:

add_action('woocommerce_before_shop_loop_item', 'loop_product_category_image', 7);

或在产品中:

add_action('woocommerce_before_main_content', 'cylkow_loop_product_category_image', 7);

或任何你想要的地方。

您还可以使用条件标签 https://woocommerce.com/document/conditional-tags/

于 2021-11-24T11:51:43.403 回答
-1

使用此代码可能会对您有所帮助。我已经通过了 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">';
   ?>
于 2014-08-14T09:51:33.383 回答