代码之家  ›  专栏  ›  技术社区  ›  Kenshinh

获取Woomerce产品类别图像URL

  •  2
  • Kenshinh  · 技术社区  · 6 年前

    我使用了下面的代码来获取Woomerce产品类别的缩略图URL,但它只输出 <img> 标记为 src="unknown" 是的。

    $cat_slug = t-shirts;
    $thumbnail_id = get_woocommerce_term_meta( $cat_slug, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );
    echo '<img src="'.$image.'" alt="" width="50" height="50" />'; 
    

    什么是让它发挥作用的最佳方法?

    编辑

    在第二次调用jean类别的缩略图时,它只输出 <img src(unknown) alt="" width="50" height="50" /> 是的。

    <div class="list-item">
        <div class="item-img">
    
            <?php
    
            $term_slug    = 't-shirts';
            $taxonomy     = "product_cat";
            $term_id      = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
            $thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
            $image        = wp_get_attachment_url( $thumbnail_id );
    
            echo '<img src="'.$image.'" alt="" width="50" height="50" />';
    
            ?>
    
    </div>
    
    <a href="#">
        <div class="item-name">
            <?php if( $term = get_term_by('slug', 't-shirts', 'product_cat') ) echo $term->name;?>
        </div>
    </a>
    
    </div>
    
    <div class="list-item">
        <div class="item-img">
    
            <?php
    
            $term_slug    = 'jeans';
            $taxonomy     = "product_cat";
            $term_id      = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
            $thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
            $image        = wp_get_attachment_url( $thumbnail_id );
    
            echo '<img src="'.$image.'" alt="" width="50" height="50" />';
    
            ?>
    
    </div>
    
    <a href="#">
        <div class="item-name">
        <?php if( $term = get_term_by('slug', 'jeans', 'product_cat') ) echo $term->name;?>
        </div>
    </a>
    
    </div>
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   LoicTheAztec    6 年前

    功能 get_woocommerce_term_meta() 需要术语id而不是术语slug。所以你可以用 get_term_by() wordpress函数从术语slug获取术语id。

    所以你的密码是:

    $term_slug    = 't-shirts';
    $taxonomy     = 'product_cat';
    $term_id      = get_term_by( 'slug', $term_slug, $taxonomy )->term_id;
    $thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
    $image        = wp_get_attachment_url( $thumbnail_id );
    
    // Output
    echo '<img src="'.$image.'" alt="" width="50" height="50" />';
    

    测试和工作


    添加版本3 (与您的评论相关)

    我使用foreach循环进行了一些其他更改,优化了代码并允许您 添加任意数量的产品类别段塞 .

    我还添加了术语链接,并做了一些小改动。

    <?php
    $term_slugs   = array('jeans', 't-shirts');
    $taxonomy     = "product_cat";
    
    // Loop though the term slugs array
    foreach ( $term_slugs as $term_slug ):
        $term        = get_term_by( 'slug', $term_slug, $taxonomy );
        if( $term ):
            $term_link   = get_term_link( $term, $taxonomy );
    
            $thumb_id    = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
            $img_src     = wp_get_attachment_url( $thumb_id );
            ?>
            <div class="list-item">
                <div class="item-image">
                    <img src="<?php echo $img_src; ?>" alt="" width="50" height="50" />
                </div>
                <div class="item-name">
                    <a href="<?php echo $term_link; ?>"><?php echo $term->name; ?></a>
                </div>
            </div>
        <?php endif;
    endforeach; ?>
    
        2
  •  2
  •   Sudharshan Nair    6 年前

    get_woocommerce_term_meta term_id 作为第一个参数。参考 Here

    像这样的代码

    $termId = 1;
    $thumbnail_id = get_woocommerce_term_meta( $termId, 'thumbnail_id', true );
    

    或者

    要从slug name获取缩略图,必须使用 get_term_by 是的。你可以参考 here

    $termName = 't-shirts';
    $category = get_term_by('name', $termName, 'product_cat');
    
    $termId = $category->term_id;
    $thumbnail_id = get_woocommerce_term_meta( $termId, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );
    echo '<img src="'.$image.'" alt="" width="50" height="50" />';