使用 woocommerce 显示 WordPress 父类别的子类别 [英] Displays the child category of WordPress's parent category with woocommerce
问题描述
如果您转到任何类别或子类别,将显示该类别的父类别的所有子类别.我在 stackoverflow 中找到了一些代码.工作正常.但是这里有一个问题.我想显示每个类别的类别缩略图.如何将其添加到此代码中?或者它可以用任何其他方法完成.有什么想法吗?
if( is_product_category() ){$queried_object = get_queried_object();$child_terms = get_term_children ($queried_object->term_id, 'product_cat' );$based_term = (is_wp_error($child_terms) || empty($child_terms)) ?get_term ($queried_object->parent, 'product_cat' ) : $queried_object;printf( '<h2 class="shop__sidebar-heading"><a href=%s?so64231449=true">%s</a></h2>', esc_url(get_term_link($based_term->term_id)), $based_term->name);$display_terms = get_terms( 'product_cat', array('orderby' =>'姓名','订单' =>'ASC','hide_empty' =>错误的,'父母' =>$based_term->term_id,) );if( !empty($display_terms) && !is_wp_error($display_terms) ){;回声'';foreach( $display_terms 作为 $display_term ){打印输出(/* '<h6%s><a href=%s">%s <span class=count">%s</span></a></h6>', */'<div class="col-md-3"><div class="content-inner"><h6%s><a href=%s">%s</a></h6></div></div>',($display_term->term_id == $queried_object->term_id) ?'类=活动"': '',esc_url(get_term_link($display_term->term_id)),$display_term->name,/* number_format($display_term->count) */);}回声'';}}
使用 WC get_woocommerce_term_meta
获取 term_meta 你可以在使用元键 thumbnail_id
中找到缩略图 ID.并使用 WP wp_get_attachment_url
获取图像 url.检查下面的代码.
if( is_product_category() ){$queried_object = get_queried_object();$child_terms = get_term_children ( $queried_object->term_id, 'product_cat' );$based_term = (is_wp_error($child_terms) || empty($child_terms)) ?get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;printf('<h2 class="shop__sidebar-heading"><a href="%s?so64231449=true">%s</a></h2>', esc_url(get_term_link($based_term->term_id)), $based_term->name );$display_terms = get_terms( 'product_cat', array('orderby' =>'姓名','订单' =>'ASC','hide_empty' =>错误的,'父母' =>$based_term->term_id,) );if( !empty( $display_terms ) && !is_wp_error( $display_terms ) ){回声'';foreach( $display_terms 作为 $display_term ){$thumbnail_id = get_term_meta ($display_term->term_id, 'thumbnail_id', true);$image = wp_get_attachment_url( $thumbnail_id );打印输出('<div class="col-md-3"><div class="content-inner"><h6%s><a href=%s">%s</a></h6><img src="%s";alt="/>
',($display_term->term_id == $queried_object->term_id) ?'类=活动"': '',esc_url(get_term_link($display_term->term_id)),$display_term->name,$图像,);}回声'';}}
f you go to any category or sub-category, all the child categories of the parent category of that category will show. I found some code in the stackoverflow. Works fine. But there is a problem here. I want to display category thumbnails with each category. How do I add it to this code? Or it can be done in any other method. Any ideas?
if( is_product_category() ){
$queried_object = get_queried_object();
$child_terms = get_term_children (
$queried_object->term_id, 'product_cat' );
$based_term = (is_wp_error($child_terms) || empty($child_terms)) ? get_term (
$queried_object->parent, 'product_cat' ) : $queried_object;
printf( '<h2 class="shop__sidebar-heading">
<a href="%s?so64231449=true">%s</a>
</h2>', esc_url(get_term_link($based_term->term_id)), $based_term->name );
$display_terms = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'parent' => $based_term->term_id,
) );
if( !empty($display_terms) && !is_wp_error($display_terms) ){
;echo '';
foreach( $display_terms as $display_term ){
printf(
/* '<h6%s><a href="%s">%s <span class="count">%s</span></a></h6>', */
'<div class="col-md-3"><div class="content-inner"><h6%s><a href="%s">%s</a></h6></div></div>',
($display_term->term_id == $queried_object->term_id) ? ' class="active"' : '',
esc_url(get_term_link($display_term->term_id)),
$display_term->name,
/* number_format($display_term->count) */
);
}
echo '';
}
}
Use WC get_woocommerce_term_meta
to get term_meta you can find thumbnail id in using meta key thumbnail_id
. and use WP wp_get_attachment_url
to get image url. check the below code.
if( is_product_category() ){
$queried_object = get_queried_object();
$child_terms = get_term_children ( $queried_object->term_id, 'product_cat' );
$based_term = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;
printf( '<h2 class="shop__sidebar-heading"><a href="%s?so64231449=true">%s</a></h2>', esc_url(get_term_link($based_term->term_id)), $based_term->name );
$display_terms = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'parent' => $based_term->term_id,
) );
if( !empty( $display_terms ) && !is_wp_error( $display_terms ) ){
echo '';
foreach( $display_terms as $display_term ){
$thumbnail_id = get_term_meta ($display_term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url( $thumbnail_id );
printf(
'<div class="col-md-3">
<div class="content-inner">
<h6%s><a href="%s">%s</a></h6>
<img src="%s" alt="" />
</div>
</div>',
($display_term->term_id == $queried_object->term_id) ? ' class="active"' : '',
esc_url(get_term_link($display_term->term_id)),
$display_term->name,
$image,
);
}
echo '';
}
}
这篇关于使用 woocommerce 显示 WordPress 父类别的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!