php 如何显示 Woocommerce 类别图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12717112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display Woocommerce Category image?
提问by MrRoman
I use this code in PHP:
我在 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" />';
Where 147is the current ID manually set, but i need to current id in other categories
147手动设置当前ID在哪里,但我需要在其他类别中设置当前ID
Any suggest?
有什么建议吗?
回答by doublesharp
To display the category image for the currently displayed category in archive-product.php, use the current category term_idwhen is_product_category()is true:
要在 中显示当前显示类别的类别图像,当为 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_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' />";
}
回答by Appic SandBox
get_woocommerce_term_meta is depricated since Woo 3.6.0.
get_woocommerce_term_meta 自 Woo 3.6.0 起已弃用。
so change
所以改变
$thumbnail_id = get_woocommerce_term_meta($value->term_id, 'thumbnail_id', true );
into: ($value->term_id should be woo category id)
进入:($value->term_id 应该是 woo 类别 id)
get_term_meta($value->term_id, 'thumbnail_id', true)
see docs for details: https://docs.woocommerce.com/wc-apidocs/function-get_woocommerce_term_meta.html
有关详细信息,请参阅文档:https: //docs.woocommerce.com/wc-apidocs/function-get_woocommerce_term_meta.html
回答by Rizwan Shamsher Kaim Khani
You may also used foreach loop for display category image and etc from parent category given by parent id.
您还可以使用 foreach 循环来显示由父 id 给出的父类别中的类别图像等。
for example, i am giving 74 id of parent category, then i will display the image from child category and its slug also.
例如,我给出了父类别的 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; ?>**
回答by Dampas
From the 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="" />'; } } }
// 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="" />'; } } }
回答by ruttoa
To prevent full size category images slowing page down, you can use smaller images with wp_get_attachment_image_src():
为了防止全尺寸类别图像减慢页面速度,您可以使用较小的图像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>';
?>
EDIT: Fixed variable name and missing quote
编辑:固定变量名称和缺少引号
回答by Kuldeep kumar
Add code in /wp-content/plugins/woocommerce/templates/loop path
在/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='' />";
}
?>
回答by Ashish Sharma
<?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
?>
回答by Sanoj Sharma
Use this code this may help you.i have passed the cat id 17.pass woocommerce cat id and thats it
使用此代码这可能对您有所帮助。我已经通过了猫 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">';
?>

