我试图在wordpress的索引文件上编写php代码。我必须得到图像&每个类别的名称, 这是我到目前为止所得到的:
$term = get_queried_object()->term_id;
$termid = get_term($term, 'product_cat' );
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'child_of' => $term,
'parent' =>0
);
$subproducts = get_terms( 'product_cat', $args);
foreach ($subproducts as $subproduct) {
echo $subproduct->name.'<br>';
echo $subproduct->term_id; //get the category Id
}
如何获取类别图片网址?
答案 0 :(得分:0)
将您的代码更改为此
foreach ($subproducts as $subproduct): setup_postdata($subproduct);
the_title();
the_ID();
the_post_thumbnail();
endforeach;
答案 1 :(得分:0)
您需要使用get_woocoomerce_term_meta
并从中获取thumbnail_id
,如下所示更改循环将完成工作
foreach( $subproducts as $subproduct ) {
echo $subproduct->name.'<br>';
echo $subproduct->term_id; //get the category Id
$thumbnail_id = get_woocommerce_term_meta( $subproduct->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="762" height="365" />';
}