在Woocommerce产品页面上显示产品类别缩略图

时间:2018-11-26 14:10:19

标签: php wordpress woocommerce thumbnails custom-taxonomy

我想在产品上添加类别父级的图片,我设法恢复了对类别的描述 而不是图片

我们非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

以下基于woocommerce_subcategory_thumbnail()源代码的功能将显示类别ID,名称或子类别中的任何产品类别缩略图:

/**
 * Display product category thumbnail.
 *
 * @param mixed $product_category Category term Id, term name or term slug.
 */

function display_product_category_thumbnail( $product_category ) {
    $taxonomy = 'product_cat';

    if( term_exists( $product_category, $taxonomy ) ) {
        if( is_numeric($product_category) )
            $field_type = 'term_id';
        else
            $field_type = 'slug';
    } else
        return;

    $term = get_term_by( $field_type, sanitize_title( $product_category ), 'product_cat' );

    $small_thumb_size = 'woocommerce_thumbnail';
    $dimensions           = wc_get_image_size( $small_thumb_size );

    if ( $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) ) {
        $image        = wp_get_attachment_image_src( $thumbnail_id, $small_thumb_size );
        $image        = $image[0];
        $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumb_size ) : false;
        $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumb_size ) : false;
    } else {
        $image        = wc_placeholder_img_src();
        $image_srcset = false;
        $image_sizes  = false;
    }

    if ( $image ) {
        // Prevent esc_url from breaking spaces in urls for image embeds.
        // Ref: https://core.trac.wordpress.org/ticket/23605.
        $image = str_replace( ' ', '%20', $image );

        // Add responsive image markup if available.
        if ( $image_srcset && $image_sizes ) {
            echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $term->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
        } else {
            echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $term->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
        }
    }
}

经测试可正常工作


更新-添加 (与您的评论有关)

要从WC_Product对象$product获取产品类别并显示其说明和缩略图,您将使用:

global $product;

$term_ids = $product->get_category_ids();

// Loop through product category IDs
foreach( $term_ids as $term_id ){
    $term = get_term ( $term_id, 'product_cat' );

    // Product category name
    echo '<p>' . $term->name . '</p>';

    // Product category description
    echo '<p>' . $term->description . '</p>';

    // Product category description
    display_product_category_thumbnail( $term_id )
}

经测试可正常工作

  

注意: $product->get_categories();已被过时和淘汰,并提供了格式化产品类别的列表,但这并不是最佳的有效方法。

     

有关信息:get_categories()方法已由函数wc_get_product_category_list()

取代

答案 1 :(得分:0)

@LoicTheAztec 实际上,为了显示说明,我制作了一个片段

global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description;

我认为使用echo echo thumbnail_id可以,但是没有!谢谢

答案 2 :(得分:0)

@LoicTheAztec 你好, 谢谢您的建议和解释

我想要在产品页面中显示图像和类别说明。

我有一个类别艺术家,并且在每个产品的页面上我都想显示图像和说明

但是产品分为几类,我想显示其子类别

例如:我在artiste类别中都有,而在子类别中则有艺术家的姓名

在我的产品页面中,我将显示艺术家的图像和描述

这是我所做的: 在我的产品页面中,我创建了一个带有acf的字段,用于检索我的子类别

在产品模板中,我制作了一个片段

global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description;

可以的显示说明

当我使用您的函数时,我的页面仍然保持加载状态,所以我做错了什么,但是怎么办?

相关问题