woocommerce - 从mysql获取类别图像

时间:2016-04-12 11:27:14

标签: mysql wordpress woocommerce

我想在woocommerce中从mysql数据库返回我的类别图像。我不知道该怎么做以及如何从数据库中返回图像。

到目前为止,我只返回类别名称和Ides,

SELECT * from `wp_terms` where term_id in (SELECT term_id FROM `wp_term_taxonomy` WHERE `taxonomy` LIKE 'product_cat' AND `parent` = 0 and count>0)

我该怎么办?

2 个答案:

答案 0 :(得分:3)

Try below code:

    $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC'));
    foreach($catTerms as $catTerm) :
    $thumbnail_id = get_woocommerce_term_meta( $catTerm->term_id, 'thumbnail_id', true ); 
    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id );
    <li>
        <img src="<?php echo $image; ?>" width="152" height="245"/>
        <span><?php echo $catTerm->name; ?></span>
    </li>
    endforeach;

答案 1 :(得分:0)

显示当前显示的类别的类别图像 -

// verify that this is a product category page
if ( is_product_category() ){
    global $wp_query;

    // get the query object
    $cat = $wp_query->get_queried_object();

    // get the thumbnail id using the queried category term_id
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 

    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id ); 

    // print the IMG HTML
    echo "<img src='{$image}' alt='' width='762' height='365' />";
}