MrR*_*man 20 php wordpress thumbnails woocommerce
我在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" />';
Run Code Online (Sandbox Code Playgroud)
其中147是当前的ID手动设置,但我需要在其他类别电流id
有什么建议?
dou*_*arp 45
要显示当前显示的类别的类别图像,请在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' />";
}
Run Code Online (Sandbox Code Playgroud)
小智 7
get_woocommerce_term_meta 自 Woo 3.6.0 起已弃用。
所以改变
$thumbnail_id = get_woocommerce_term_meta($value->term_id, 'thumbnail_id', true );
Run Code Online (Sandbox Code Playgroud)
进入:($value->term_id 应该是 woo 类别 id)
get_term_meta($value->term_id, 'thumbnail_id', true)
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅文档:https : //docs.woocommerce.com/wc-apidocs/function-get_woocommerce_term_meta.html
Run Code Online (Sandbox Code Playgroud)// 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="" />'; } } }
为了防止全尺寸类别图像减慢页面速度,您可以使用较小的图像wp_get_attachment_image_src():
<?php
$thumbnail_id = get_term_meta( $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>';
?>
Run Code Online (Sandbox Code Playgroud)
编辑:修复变量名称和缺少引号
| 归档时间: |
|
| 查看次数: |
82620 次 |
| 最近记录: |