我正在制作我的第一个主题,感谢 The Loop 和 WooCommerce SDK 提供的所有帮助,一切都进展得非常快。然后今天我花了一整天的时间,没有做一些像显示图像这样简单的事情......经过一整天的努力,我唯一学到的是 WP 似乎没有提供获取类别图像的方法,而且很多人都有问这个问题很多年了,但我仍然找不到真正做到这一点的方法...... :(
我想要做的是在我的商店上方创建一个滑块,显示精选商店类别的图像。我希望能够输入术语名称列表,并基于此我的函数应以类别图像的形式生成指向产品类别的链接。
听起来很简单...事实证明,它远非简单...在您将其标记为重复问题之前,让我解释一下为什么我要问它...
我发现的一些解决方案要求我知道术语的 ID,而不是术语名称。有人说“使用自定义术语搜索获取 ID”,但没有解释如何操作。我知道如何对帖子进行基于自定义分类的查询,但不知道如何对术语进行查询。该文档让我对传递给查询项的值感到困惑:(
其他解决方案要求我首先找到特定类别的产品,然后从那里向后查找产品的类别图像(???)
当然,人们喜欢对所有事情给出默认答案:“哦,你正在设计自己的主题并想要显示类别图标?很简单,只需下载一个插件即可为你做到这一点”。现在为什么我没有想到将其他人的插件包含到我的主题中? (捂脸)
其他答案只是显示如何打印术语名称列表,但到目前为止,没有什么能够让我做应该那么简单的事情:
$categories = array("software", "plugins", "merch");
foreach($categories as $cat) {
$term_id = get_term_id($cat);
$term_image = get_term_featured_image($term_id);
echo '<img src="'.$term_image.'">;
}
获取术语的第一个问题是获取术语 id 的 wordpress 函数仅适用于类别分类法,但我需要查询 WooCommerce 的 Product_cat 分类法。其次,即使您有 ID,似乎也没有获取缩略图/特色图像的选项。那么现在怎么办?
所以我进入低级别并开始直接使用 $wpdb 查询表,我确定我要查找的术语有 term_id 94。我在 termmeta 表中查询缩略图的帖子 ID,发现它是 905。现在我转向我的帖子表并找到...没有帖子条目 905!搞什么?因此,我对另外两个类别进行了此操作,并发现了相同的内容。查找图像的 id 会导致尝试提取帖子附件时不会返回任何内容,因为没有帖子与图像的 id 匹配...
为什么这这么难? WordPress 让其他一切都变得非常简单,但这个听起来简单的任务似乎几乎不可能完成......所以现在你看到我已经在谷歌上搜索到这个并且已经在我的背部挣扎,我出于绝望而问这个问题:
如何获取 Product_cat 术语名称数组并将其转换为 url 数组以显示类别的图像?
谢谢
最短的方法是使用
woocommerce_subcategory_thumbnail()
专用功能:
$product_categories = array("software", "plugins", "merch");
// Loop through the product categories
foreach( $product_categories as $category ) {
// Get the WP_term object
$term = get_term_by( 'slug', sanitize_title( $category ), 'product_cat' );
// Get the term link (if needed)
$term_link = get_term_link( $term, 'product_cat' );
// Display the product category thumbnail
woocommerce_subcategory_thumbnail( $term );
}
另一种逐步方式,将显示链接的产品类别图像及其名称:
$product_categories = array("software", "plugins", "merch");
// Loop through the product categories
foreach( $product_categories as $category ) {
// Get the WP_term object
$term = get_term_by( 'slug', sanitize_title( $category ), 'product_cat' );
// Get the term link (if needed)
$term_link = get_term_link( $term, 'product_cat' );
// Get the thumbnail Id
$thumbnail_id = (int) get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
if( $thumbnail_id > 0 ) {
// Get the attchement image Url
$term_img = wp_get_attachment_url( $thumbnail_id );
// Formatted thumbnail html
$img_html = '<img src="' . $term_img . '">';
} else {
$img_html = '';
}
echo '<a href="' . $term_link . '">' . $img_html . $term->name . '</a><br>';
}
两者都有效……
获取所有产品类别
WP_Term
对象并使用缩略图显示它们:
// Get all product categories
$product_category_terms = get_terms( array(
'taxonomy' => "product_cat",
'hide_empty' => 1,
));
foreach($product_category_terms as $term){
// Get the term link (if needed)
$term_link = get_term_link( $term, 'product_cat' );
## -- Output Example -- ##
// The link (start)
echo '<a href="' . $term_link . '" style="display:inline-block; text-align:center; margin-bottom: 14px;">';
// Display the product category thumbnail
woocommerce_subcategory_thumbnail( $term );
// Display the term name
echo $term->name;
// Link close
echo '</a>';
}
我同意 LoicTheAztez 的回答,这是我最近(在 woocommerce 更新后)获取任何术语的名称、缩略图和链接的方法。
// Terms thumbnail grid helper function
function get_term_details($term_name) {
// use get_terms() to get the array of names
$terms = get_terms( $term_name, array(
'orderby' => 'name', /* orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description') */
'hide_empty' => false,
) );
$term_details = array();
foreach ( $terms as $term ) {
$name = $term->name;
$thumbnail = wp_get_attachment_url( get_term_meta($term->term_id, 'thumbnail_id', true) );
$link = get_term_link( $term );
$details = array(
'name' => $name,
'thumbnail' => $thumbnail,
'link' => $link,
);
array_push($term_details, $details);
}
return $term_details;
}
这可以这样称呼:
$product_category_details = get_term_details('product_cat');