获取 WooCommerce 中的所有产品类别
Get all product categories in WooCommerce
我正在尝试在 WooCommerce 中获取产品类别,但 get_terms()
功能对我不起作用。我得到一个空数组。
我做错了什么?如何获取所有 Woocommerce 产品类别术语?
产品类别是 WooCommerce 产品使用的 "custom taxonomy" product_cat
。
您需要使用get_terms()
,使用正确的分类法,这样:
// Get Woocommerce product categories WP_Term objects
$categories = get_terms( ['taxonomy' => 'product_cat'] );
// Getting a visual raw output
echo '<pre>'; print_r( $categories ); echo '</pre>';
您还可以使用 get_terms()
获取空产品类别,例如:
$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
经过测试并有效(WordPress 3.5+ 和 WooCommerce 2.4+)……两者都应该适合您。
你会得到类似的东西:
Array
(
[0] => WP_Term Object
(
[term_id] => 83
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 83
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 5
[filter] => raw
[meta_value] => 1
)
[2] => WP_Term Object
(
[term_id] => 11
[name] => Tshirts
[slug] => tshirts
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
[meta_value] => 2
)
// … and so on …
)
我从所有产品类别中取出我的,也排除了我不想显示的类别
$taxonomy = "product_cat";
$terms = get_terms($taxonomy, array('orderby' => 'slug', 'hide_empty' => false, 'exclude' => array( 19 ))); //Exclude Specific Category by ID
foreach ($terms as $term) {
$thumbnail_id = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id); ?>
<div class="col-12 col-md-3">
<div class="cat-item drop-shadow white-b padding-20 align-center margin-bottom-30">
<?php
echo '<h3 class="uppercase strong blue-2-c margin-bototm-20 equal-height">' . $term->name . '</h3>';
echo '<div class="item-thumbnail" style="background: url('.$image.');height:150px"></div>';
echo '<a href="' . get_term_link($term->name, $taxonomy) . '" class="button color-blue-2-radial">View Range</a>';
?>
</div>
</div>
我正在尝试在 WooCommerce 中获取产品类别,但 get_terms()
功能对我不起作用。我得到一个空数组。
我做错了什么?如何获取所有 Woocommerce 产品类别术语?
产品类别是 WooCommerce 产品使用的 "custom taxonomy" product_cat
。
您需要使用get_terms()
,使用正确的分类法,这样:
// Get Woocommerce product categories WP_Term objects
$categories = get_terms( ['taxonomy' => 'product_cat'] );
// Getting a visual raw output
echo '<pre>'; print_r( $categories ); echo '</pre>';
您还可以使用 get_terms()
获取空产品类别,例如:
$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
经过测试并有效(WordPress 3.5+ 和 WooCommerce 2.4+)……两者都应该适合您。
你会得到类似的东西:
Array
(
[0] => WP_Term Object
(
[term_id] => 83
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 83
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 5
[filter] => raw
[meta_value] => 1
)
[2] => WP_Term Object
(
[term_id] => 11
[name] => Tshirts
[slug] => tshirts
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
[meta_value] => 2
)
// … and so on …
)
我从所有产品类别中取出我的,也排除了我不想显示的类别
$taxonomy = "product_cat";
$terms = get_terms($taxonomy, array('orderby' => 'slug', 'hide_empty' => false, 'exclude' => array( 19 ))); //Exclude Specific Category by ID
foreach ($terms as $term) {
$thumbnail_id = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id); ?>
<div class="col-12 col-md-3">
<div class="cat-item drop-shadow white-b padding-20 align-center margin-bottom-30">
<?php
echo '<h3 class="uppercase strong blue-2-c margin-bototm-20 equal-height">' . $term->name . '</h3>';
echo '<div class="item-thumbnail" style="background: url('.$image.');height:150px"></div>';
echo '<a href="' . get_term_link($term->name, $taxonomy) . '" class="button color-blue-2-radial">View Range</a>';
?>
</div>
</div>