在Woocommerce中获取并显示产品类别特色图片 [英] Get and display the product category featured image in Woocommerce

查看:117
本文介绍了在Woocommerce中获取并显示产品类别特色图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我的第一个主题,感谢The Loop和WooCommerce的SDK提供的所​​有帮助,一切进展都非常快。直到今天,我整天都没有做像显示图像那样简单的事情……经过一整天的努力,我唯一学会的是WP似乎没有提供获取类别图像的方法,许多人已经问了这个问题很多年了,仍然无法找到正确的方法...
:(



我想做的就是创建我商店上方的滑块,用于显示精选的商店类别的图像,我希望能够输入术语名称列表,并基于此,我的功能应以类别图像的形式生成指向产品类别的链接。



听起来很简单...事实证明,离简单很近...在您将其标记为重复问题之前,让我解释一下我为什么问...



我发现的某些解决方案要求我知道该术语的ID,而不是tern名称。有人说使用自定义术语获取ID搜索,但没有说明如何操作。我知道如何对帖子(而不是术语)进行基于自定义分类的查询。该文档使我难以理解要传递给查询词的值:(



其他解决方案要求我首先找到特定类别的产品,然后再找到该产品的类别图片从那里倒退(????)



然后当然会有人们喜欢为所有内容提供的默认答案:哦,您正在设计自己的主题,要显示类别图标?简单,只需下载一个插件即可为您做到这一点。现在,为什么我不想到只将别人的插件包含到我的主题中?(facepalm)



其他答案仅显示了如何打印术语名称列表,但到目前为止,没有任何内容可以让我做 应该 这样简单的事情:

  $ categories = array( software, plugins, merch); 
foreach($类别为$ cat){
$ term_id = get_term_id($ cat);
$ term_image = get_term_featured_image($ term_id);
echo'< img src ='。$ term_image。’> ;;
}

获取术语的第一个问题是wordpress函数获取术语id仅适用于类别分类法,但我需要查询WooCommerce的product_cat分类法。其次,即使您有ID,似乎也无法选择提取缩略图/功能齐全的图像。那么,现在是什么呢?



所以我进入了较低的级别,开始直接使用$ wpdb查询表,并确定所使用的术语具有term_id94。我查询termmeta表缩略图的帖子ID,我发现它是905。现在转到我的帖子表,然后找到....没有帖子条目905! WTF?因此,我针对另外两个类别执行此操作,并找到了相同的内容。查找图像的ID不会导致尝试提取帖子的附件没有任何回报,因为没有帖子与图像的ID匹配...



为什么这么难? ? WordPress使其他一切都变得如此简单,但这项简单的任务似乎几乎无法完成……因此,现在您看到我已经将Google查到死了,并且已经在挣扎了,我无奈地问了这个问题: / p>

如何获取product_cat术语名称数组并将其转换为url数组以显示类别的图像?



谢谢

解决方案

最简单的方法是使用 woocommerce_subcategory_thumbnail()专用功能:

  $ product_categories = array( software, plugins, merch); 

//遍历产品类别
foreach($ product_categories as $ category){
//获取WP_term对象
$ term = get_term_by('slug' ,sanitize_title($ category),'product_cat');

//获取术语链接(如果需要)
$ term_link = get_term_link($ term,‘product_cat’);

//显示产品类别缩略图
woocommerce_subcategory_thumbnail($ term);
}

另一种逐步显示方式,将显示带有其名称:

  $ product_categories = array( software, plugins, merch); 

//遍历产品类别
foreach($ product_categories as $ category){
//获取WP_term对象
$ term = get_term_by('slug' ,sanitize_title($ category),'product_cat');

//获取术语链接(如果需要)
$ term_link = get_term_link($ term,‘product_cat’);

//获取缩略图ID
$ thumbnail_id =(int)get_woocommerce_term_meta($ term-> term_id,‘thumbnail_id’,true);

if($ thumbnail_id> 0){
//获取附件图片网址
$ term_img = wp_get_attachment_url($ thumbnail_id);

//格式化的缩略图html
$ img_html =‘< img src =’。$ term_img。’>’;
} else {
$ img_html =‘’;
}
echo‘< a href =’。$ term_link。’>’。 $ img_html。 $ term-> name。 ’< / a>< br>’;
}

两者均有效……



< hr>

要获取所有产品类别 WP_Term 对象并显示其缩略图:

  //获取所有产品类别
$ product_category_terms = get_terms(array(
'taxonomy'=> product_cat,
' hide_empty'=> 1,
));

foreach($ product_category_terms as $ term){
//获取术语链接(如果需要)
$ term_link = get_term_link($ term,‘product_cat’);

##-输出示例-##

//链接(开始)
echo'< a href ='。$ term_link。 ' style = display:inline-block; text-align:center; margin-bottom:14px;>';

//显示产品类别缩略图
woocommerce_subcategory_thumbnail($ term);

//显示术语名称
echo $ term-> name;

//链接关闭
echo‘< / a>’;
}


I'm making my first theme and everything is progressing real fast thanks to all the assistance offered by The Loop and WooCommerce's SDK. Then today I spent an entire day failing to do something as simple as showing an image... After an entire day of struggling the only things I managed to learn is that WP seems to offer NO means for fetching a category's image and MANY people have asked this question for many years and STILL I can't find a way to ACTUALLY do it... :(

What I want to do is create a slider above my store that shows the images of a curated selection of shop categories. I want to be able to enter a list of term names and based on that my function should generate links to the product categories in the form of the category's image.

Sounds simple... turns out, it's nowhere near CLOSE to simple... Before you go off and mark this as a duplicate question, let me explain why I am asking it...

Some of the solutions I have found require that I know the term's IDs, not the tern name. Some say "Get the id's using a custom term search" but doesn't explain how. I know how to do custom taxonumy based queries for posts but not for terms. the documentation confuses me in terms of what values to pass to query terms :(

Other solutions require that I first find a product of a specific category and then find the product's category image working backwards from there (????)

Then of course there is the default answer people love to give for everything: "Oh you are designing your own theme and want to show category icons? Simple, just download a plugin to do that for you". Now why didn't I think of just including someone else's plugin into my theme? (facepalm)

Other answers just show how to print the list of term names but nothing so far has been able to let me do what should have been been as simple as this:

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

First problem with getting the term is that the wordpress function to get the term id only works on the category taxonomy but I need to query WooCommerce's product_cat taxonomy. Secondly there doesn't seem to be an option to fetch the thumbnail/featured image even if you HAVE an id. So now what?

So I went low level and started querying the tables directly using $wpdb and I determine the term I am after has term_id 94. I query the termmeta table for the thumbnail's post ID and I find it is 905. Now I turn to my posts table and find.... there IS no post entry 905! WTF? So I do this for two more categories and find the same thing. Finding the image's id results in nothing being returned from the attempt at extracting the post's attachments since there IS no post matching the image's id...

Why is this so darned hard? WordPress makes everything else so incredibly simple yet this simple sounding task seems to be near impossible to do... so now that you see I have Googled this to death and struggled my backside off already, I am asking this question out of desperation:

How do I take an array of product_cat term names and convert that into an array of url's to display the category's image?

Thanks

解决方案

The shortest way is to use woocommerce_subcategory_thumbnail() dedicated function:

$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 );
}

The other step by step way, that will display the linked product category image with its name:

$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>';
}

Both works…


To get all product categories WP_Term objects and display them with their thumbnails:

// 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>';
}

这篇关于在Woocommerce中获取并显示产品类别特色图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆