如何使用缩略图限制退回的产品类别 (WooCommerce)
How to limit returned product categories with thumbnails (WooCommerce)
我的目标是仅显示类别 ID 93(供应商)下的 child 个类别,并带有缩略图和 URL,以便人们可以单击供应商徽标并查看该类别中的其他产品。我的所有东西大部分都在工作,但我不确定如何限制我的请求以仅显示我的 parent 中的一个 child。这无疑是非常业余的——我不是后端开发人员,也不是真正了解如何编写 PHP.
<?php
echo $wp_query;
$terms_post = get_the_terms($product->ID, 'product_cat');
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
$category_url = get_term_link( $term_cat_id, 'product_cat', true );
$thumbnail_id = get_woocommerce_term_meta($term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<a href="'. $category_url .'"><img src="' . $image_url . '" alt="" width="50" height="50"></a>';
}
?>
要仅显示 parent 中的 1 child,只需使用 array_slice()。
foreach(array_slice($terms_post, 0, 1) as $term_cat ) {
$term_cat_id = $term_cat->term_id;
$category_url = get_term_link( $term_cat_id, 'product_cat', true );
$thumbnail_id = get_woocommerce_term_meta($term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<a href="'. $category_url .'"><img src="' . $image_url . '" alt="" width="50" height="50"></a>';
}
如果有效请告诉我。
已编辑:
使用以下代码使用 parent 类别 slug 获取 child 类别。
<?php
global $post;
$category_id = get_term_by('slug', 'PARENT-CAT-SLUG', 'product_cat');
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
if($term->parent === $category_id->term_id) { ?>
<span class="product-sub-cats"><?php echo $term->name; ?></span>
<?php break;
}
}
?>
将 "PARENT-CAT-SLUG" 替换为您的 Parent 类别的别名。
我的目标是仅显示类别 ID 93(供应商)下的 child 个类别,并带有缩略图和 URL,以便人们可以单击供应商徽标并查看该类别中的其他产品。我的所有东西大部分都在工作,但我不确定如何限制我的请求以仅显示我的 parent 中的一个 child。这无疑是非常业余的——我不是后端开发人员,也不是真正了解如何编写 PHP.
<?php
echo $wp_query;
$terms_post = get_the_terms($product->ID, 'product_cat');
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
$category_url = get_term_link( $term_cat_id, 'product_cat', true );
$thumbnail_id = get_woocommerce_term_meta($term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<a href="'. $category_url .'"><img src="' . $image_url . '" alt="" width="50" height="50"></a>';
}
?>
要仅显示 parent 中的 1 child,只需使用 array_slice()。
foreach(array_slice($terms_post, 0, 1) as $term_cat ) {
$term_cat_id = $term_cat->term_id;
$category_url = get_term_link( $term_cat_id, 'product_cat', true );
$thumbnail_id = get_woocommerce_term_meta($term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<a href="'. $category_url .'"><img src="' . $image_url . '" alt="" width="50" height="50"></a>';
}
如果有效请告诉我。
已编辑:
使用以下代码使用 parent 类别 slug 获取 child 类别。
<?php
global $post;
$category_id = get_term_by('slug', 'PARENT-CAT-SLUG', 'product_cat');
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
if($term->parent === $category_id->term_id) { ?>
<span class="product-sub-cats"><?php echo $term->name; ?></span>
<?php break;
}
}
?>
将 "PARENT-CAT-SLUG" 替换为您的 Parent 类别的别名。