【问题标题】:Get Woocommerce product category image url获取 Woocommerce 产品类别图片网址
【发布时间】:2018-07-18 09:51:52
【问题描述】:

我已使用以下代码获取 Woocommerce 产品类别的缩略图 URL,但它仅输出带有 src="unknown"<img> 标签。

$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" />'; 

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

编辑

在第二次调用牛仔裤类别的缩略图时,它只输出&lt;img src(unknown) alt="" width="50" height="50" /&gt;

<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>

【问题讨论】:

  • 只是带有 src="unknown" 的 img 标签。我已经编辑了这个问题。谢谢
  • $cat_slug = 't-shirts';。进行此更改
  • 同样的结果。它输出&lt;img src=(unknown) alt="" width="50" height="50"&gt; without the image url.

标签: php wordpress image woocommerce custom-taxonomy


【解决方案1】:

函数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" />';

经过测试并且可以工作


Addition rev 3(与您的评论相关)

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

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

<?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; ?>

【讨论】:

  • 谢谢。它工作正常。但是,我需要在同一页面上显示两个类别的缩略图。所以我两次使用了这段代码,第二次我把$term_slug改成了'jeans';。但问题是没有显示牛仔裤的类别图片。它输出&lt;img src(unknown) alt="" width="50" height="50" /&gt;。非常感谢任何帮助
  • 我刚刚添加了代码作为编辑。抱歉,标记太长了!
  • @sparkinglabs 我再次更新了答案末尾的添加内容(修订版 3)。现在还有术语链接和一些小的更改。
【解决方案2】:

get_woocommerce_term_metaterm_id 作为第一个参数。参考Here

类似这样的代码

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

要从 slug 名称中获取缩略图,您必须使用 get_term_by 获取术语 ID。可以参考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" />';

【讨论】:

  • 就是这样。谢谢。有什么办法可以在那里使用蛞蝓?
  • 仅供参考,Woocommerce 产品类别自定义分类不是“类别”而是 “product_cat”。 WordPress 在普通帖子(如博客帖子)上使用分类“类别”......
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 2021-08-05
相关资源
最近更新 更多