wordpress 获取 Woocommerce 类别缩略图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13606138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Woocommerce Category Thumbnails
提问by topherg
I have a custom template for a woocommerce category page to only display the categories. I have got the system to get a list of the child categories (by using get_term_children($id, 'product_cat')
and get_term_by(...)
), but it only returns objects containing all the required information, except the thumbnail data. Does anyone know how I can get the thumbnail for the term?
我有一个用于 woocommerce 类别页面的自定义模板,仅用于显示类别。我让系统获取子类别列表(通过使用get_term_children($id, 'product_cat')
和get_term_by(...)
),但它只返回包含所有必需信息的对象,缩略图数据除外。有谁知道我如何获得该术语的缩略图?
回答by topherg
Sorted it, here's the code I used:
排序它,这是我使用的代码:
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
回答by Eh Jewel
If the get_woocommerce_term_meta()
function does not work for you then you can try the get_term_meta()
function instead.
如果该get_woocommerce_term_meta()
功能对您不起作用,那么您可以尝试使用该get_term_meta()
功能。
You can get the WooCommerce product category thumbnail with the following code-
您可以使用以下代码获取 WooCommerce 产品类别缩略图-
<?php
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id ); // This variable is returing the product category thumbnail image URL.
回答by Derek
Had a similar setup but when I used what you did I didnt actually get the thumbnail file I got the full image file so instead I used this: wp_get_attachment_thumb_url so that my output url would be "../my-images"/image-150x150.jpg" and actually got it to pull the thumbnail image, just incase anyone runs into a similar situation..
有一个类似的设置但是当我使用你所做的我实际上并没有得到缩略图文件我得到了完整的图像文件所以我使用了这个:wp_get_attachment_thumb_url 这样我的输出 url 就会是“../my-images”/image-150x150 .jpg”并实际上让它拉出缩略图,以防万一有人遇到类似的情况..
回答by Omid Ahmadyani
<?php
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_bid', true );
$image_url = wp_get_attachment_url( $thumbnail_id ); // This variable is returing the product category thumbnail image URL.
notice: get_woocommerce_term_meta is deprecated
注意:不推荐使用 get_woocommerce_term_meta