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

Php 在Woocommerce产品页面上显示产品类别缩略图,php,wordpress,woocommerce,thumbnails,custom-taxonomy,Php,Wordpress,Woocommerce,Thumbnails,Custom Taxonomy,我想在我的产品上添加类别父级的图像,我设法恢复类别的描述 但不是图片 非常感谢您的帮助。基于woocommerce\u subcategory\u thumbnail()源代码的以下函数将显示类别Id、名称或slug中的任何产品类别缩略图: /** * Display product category thumbnail. * * @param mixed $product_category Category term Id, term name or term slug. */ fu

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


非常感谢您的帮助。

基于
woocommerce\u subcategory\u thumbnail()
源代码的以下函数将显示类别Id、名称或slug中的任何产品类别缩略图:

/**
 * 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'] ) . '" />';
        }
    }
}
测试和工作

注意:已过时且不推荐使用并给出了格式化产品类别的列表,这并不是最有效的方法

有关信息:
get\u categories()
方法替换为函数
wc\u get\u product\u categories\u list()

@洛伊克塞兹泰克
事实上,为了显示描述,我做了一个片段

global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description;
我以为有了回音缩略图就行了,但是不行!谢谢你

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

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

我有一个类别的艺术家,在每个产品的页面上,我想显示图像和描述

但是这些产品分为几个类别,我想展示它的子类别

我有关于艺人的分类和艺术家的名字

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

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

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

global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description;
显示说明没关系


当我使用你的功能时,我的页面仍然处于加载状态,所以我做错了什么?

很好!感谢@LoicTheAztecIn fact显示我所做的描述片段[code]global$post$product$categ=$product->get_categories()$术语=获取术语('name',strip_标签($categ),'product_cat');echo$term->description;[\code]我以为有了回音缩略图id就行了,但是不行!感谢you@Cylvain很抱歉,这段代码基于Woocommerce源代码,经过测试,工作完美…请不要将代码放在注释中,因为它不可读,并且您的解释也不清楚。我添加了一个附加项,向您显示我答案的en处的用法。您的代码不能用作
$product->get_categories()已过时且已弃用。这还可以提供多个产品类别。我在我的回答中添加了一项内容…@LoicTheAztec没关系,很抱歉,它一直在工作,我忘记了执行操作()非常感谢您的帮助
global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description;