2 php wordpress thumbnails custom-taxonomy woocommerce
我正在制作我的第一个主题,感谢 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.'">;
}
Run Code Online (Sandbox Code Playgroud)
获取术语的第一个问题是,获取术语 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");\n\n// Loop through the product categories\nforeach( $product_categories as $category ) {\n // Get the WP_term object\n $term = get_term_by( \'slug\', sanitize_title( $category ), \'product_cat\' );\n\n // Get the term link (if needed)\n $term_link = get_term_link( $term, \'product_cat\' );\n\n // Display the product category thumbnail\n woocommerce_subcategory_thumbnail( $term );\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n另一种逐步方式,将显示链接的产品类别图像及其名称:
\n\n$product_categories = array("software", "plugins", "merch");\n\n// Loop through the product categories\nforeach( $product_categories as $category ) {\n // Get the WP_term object\n $term = get_term_by( \'slug\', sanitize_title( $category ), \'product_cat\' );\n\n // Get the term link (if needed)\n $term_link = get_term_link( $term, \'product_cat\' );\n\n // Get the thumbnail Id\n $thumbnail_id = (int) get_woocommerce_term_meta( $term->term_id, \'thumbnail_id\', true );\n\n if( $thumbnail_id > 0 ) {\n // Get the attchement image Url\n $term_img = wp_get_attachment_url( $thumbnail_id );\n\n // Formatted thumbnail html\n $img_html = \'<img src="\' . $term_img . \'">\';\n } else {\n $img_html = \'\';\n }\n echo \'<a href="\' . $term_link . \'">\' . $img_html . $term->name . \'</a><br>\';\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n两者都有效\xe2\x80\xa6
\n\n获取所有产品类别WP_Term
对象并使用缩略图显示它们:
// Get all product categories\n$product_category_terms = get_terms( array(\n \'taxonomy\' => "product_cat",\n \'hide_empty\' => 1,\n));\n\nforeach($product_category_terms as $term){\n // Get the term link (if needed)\n $term_link = get_term_link( $term, \'product_cat\' );\n\n ## -- Output Example -- ##\n\n // The link (start)\n echo \'<a href="\' . $term_link . \'" style="display:inline-block; text-align:center; margin-bottom: 14px;">\';\n\n // Display the product category thumbnail\n woocommerce_subcategory_thumbnail( $term );\n\n // Display the term name\n echo $term->name;\n\n // Link close\n echo \'</a>\';\n}\n
Run Code Online (Sandbox Code Playgroud)\n