在Woocommerce中获取并显示产品类别特色图片

时间:2018-11-10 15:35:31

标签: php wordpress woocommerce thumbnails custom-taxonomy

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

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

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

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

其他解决方案要求我首先找到特定类别的产品,然后找到从那里向后工作的产品类别图像(????)

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

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

$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_id94。我在termmeta表中查询缩略图的帖子ID,发现为905。现在我转向我的帖子表并查找...。没有帖子条目905! WTF?因此,我针对另外两个类别执行此操作,并找到了相同的内容。查找图像的ID不会导致尝试提取帖子的附件没有任何回报,因为没有帖子与图像的ID匹配...

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

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

谢谢

1 个答案:

答案 0 :(得分:1)

最快捷的方法是使用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>';
}