WooCommerce 获取属性缩略图 - Variation Swatches and Photos 插件
WooCommerce get attribute thumbnail - Variation Swatches and Photos plugin
我正在使用插件 WooCommerce Variation Swatches and Photos,它可以让我在我的产品属性中添加缩略图。
我需要列出模板上的所有属性,我还想获取并显示缩略图。
$terms = get_terms( array(
'taxonomy' => 'pa_texture',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
print_r($term);
}
缩略图功能在 WooCommerce 中不是默认的,所以当我 print_r $term 时没有缩略图 URL:
WP_Term Object
(
[term_id] => 54
[name] => Guilin
[slug] => guilin
[term_group] => 0
[term_taxonomy_id] => 54
[taxonomy] => pa_texture
[description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla imperdiet facilisis convallis.
[parent] => 0
[count] => 2
[filter] => raw
[meta_value] => 0
)
如何获取属性的缩略图?
产品类别术语 'product_cat'
分类法的经典方法是:
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$img_src = wp_get_attachment_url( $thumb_id );
echo '<p><img src="'.$img_src.'" alt="" />'.$tem->name.'</p>';
}
So may be changing the taxonomy to product attributes like 'pa_texture'
, it should do the trick
(I hope, but I am not sure as I dont use Variation Swatches and Photos plugin).
这是未经测试的,但是以下的一些变体应该可以工作:
foreach ( $terms as $key => $term ) {
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, $term->taxonomy . '_photo', true );
$terms[ $key ]->thumb = wp_get_attachment_image_src( $thumbnail_id );
print_r( $term );
}
如果你看一下the relevant plugin file,你就会知道作者是如何得到这些图像的。上面的代码大致基于此。
感谢@Und3rTow 的输入,找到了解决方案。
get_woocommerce_term_meta中正确的参数是pa_texture_swatches_id_photo
。
这是最终代码:
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'pa_texture_swatches_id_photo', true );
$textureImg = wp_get_attachment_image_src( $thumbnail_id ); ?>
<img src="<?php echo $textureImg[0]; ?>">
我在加售产品中显示图片时遇到了类似的问题。乱七八糟,但是:
if ( $products_upsells->have_posts() ) : while ( $products_upsells->have_posts() ) : $products_upsells->the_post();
$_product = wc_get_product(get_the_ID());
$attributes = $_product->get_attributes();
$attr_id = $attributes['pa_kolor']['options'][0];
$thumb_id = get_term_meta($attr_id);
$img_src = wp_get_attachment_url($thumb_id['pa_kolor_swatches_id_photo'][0] );
echo '<img src="'.$img_src.'" alt="" />';
endwhile; endif;
wp_reset_query();
查看这段代码:
$_product = wc_get_product(get_the_ID());
$attributes = $_product->get_attributes();
$attr_id = $attributes['pa_kolor']['options'][0];
$thumb_id = get_term_meta($attr_id);
$img_src = wp_get_attachment_url($thumb_id['pa_kolor_swatches_id_photo'][0] );
我正在使用插件 WooCommerce Variation Swatches and Photos,它可以让我在我的产品属性中添加缩略图。
我需要列出模板上的所有属性,我还想获取并显示缩略图。
$terms = get_terms( array(
'taxonomy' => 'pa_texture',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
print_r($term);
}
缩略图功能在 WooCommerce 中不是默认的,所以当我 print_r $term 时没有缩略图 URL:
WP_Term Object
(
[term_id] => 54
[name] => Guilin
[slug] => guilin
[term_group] => 0
[term_taxonomy_id] => 54
[taxonomy] => pa_texture
[description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla imperdiet facilisis convallis.
[parent] => 0
[count] => 2
[filter] => raw
[meta_value] => 0
)
如何获取属性的缩略图?
产品类别术语 'product_cat'
分类法的经典方法是:
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$img_src = wp_get_attachment_url( $thumb_id );
echo '<p><img src="'.$img_src.'" alt="" />'.$tem->name.'</p>';
}
So may be changing the taxonomy to product attributes like
'pa_texture'
, it should do the trick
(I hope, but I am not sure as I dont use Variation Swatches and Photos plugin).
这是未经测试的,但是以下的一些变体应该可以工作:
foreach ( $terms as $key => $term ) {
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, $term->taxonomy . '_photo', true );
$terms[ $key ]->thumb = wp_get_attachment_image_src( $thumbnail_id );
print_r( $term );
}
如果你看一下the relevant plugin file,你就会知道作者是如何得到这些图像的。上面的代码大致基于此。
感谢@Und3rTow 的输入,找到了解决方案。
get_woocommerce_term_meta中正确的参数是pa_texture_swatches_id_photo
。
这是最终代码:
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'pa_texture_swatches_id_photo', true );
$textureImg = wp_get_attachment_image_src( $thumbnail_id ); ?>
<img src="<?php echo $textureImg[0]; ?>">
我在加售产品中显示图片时遇到了类似的问题。乱七八糟,但是:
if ( $products_upsells->have_posts() ) : while ( $products_upsells->have_posts() ) : $products_upsells->the_post();
$_product = wc_get_product(get_the_ID());
$attributes = $_product->get_attributes();
$attr_id = $attributes['pa_kolor']['options'][0];
$thumb_id = get_term_meta($attr_id);
$img_src = wp_get_attachment_url($thumb_id['pa_kolor_swatches_id_photo'][0] );
echo '<img src="'.$img_src.'" alt="" />';
endwhile; endif;
wp_reset_query();
查看这段代码:
$_product = wc_get_product(get_the_ID());
$attributes = $_product->get_attributes();
$attr_id = $attributes['pa_kolor']['options'][0];
$thumb_id = get_term_meta($attr_id);
$img_src = wp_get_attachment_url($thumb_id['pa_kolor_swatches_id_photo'][0] );