在 WooCommerce 中获取顶级产品类别的自定义数组
Get a custom array of the top level products categories in WooCommerce
有什么方法可以获取 woocomerce 中的顶级产品类别列表,以便在我的主题中的自定义部分中显示它们
这是我使用的代码,但它 returns 所有类别:
function getCategoriesList() {
// prior to wordpress 4.5.0
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
// since wordpress 4.5.0
$args = array(
'taxonomy' => "product_cat",
'child_of' => 0,
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms($args);
$list = array();
foreach( $product_categories as $cat ){
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$link = get_term_link( $cat->term_id, 'product_cat' );
$list[] = array($cat->term_id, $cat->name, $image, $link);
}
return $list;
}
我最近添加了:
'child_of' => 0
但是没有变化。
如何使其仅适用于顶级产品类别?
To get it works, the missing argument is just 'parent' => 0
(but not 'child_of'
)
所以你的工作代码应该是这样的(并且return你的数组会正确:
function getProductCategoriesList() {
// since wordpress 4.5.0
$product_categories = get_terms( $args = array(
'taxonomy' => "product_cat",
'hide_empty' => false,
'parent' => 0,
) );
$list = array();
foreach( $product_categories as $cat ){
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$link = get_term_link( $cat->term_id, 'product_cat' );
$list[] = array($cat->term_id, $cat->name, $image, $link);
}
return $list;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效
有什么方法可以获取 woocomerce 中的顶级产品类别列表,以便在我的主题中的自定义部分中显示它们
这是我使用的代码,但它 returns 所有类别:
function getCategoriesList() {
// prior to wordpress 4.5.0
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
// since wordpress 4.5.0
$args = array(
'taxonomy' => "product_cat",
'child_of' => 0,
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms($args);
$list = array();
foreach( $product_categories as $cat ){
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$link = get_term_link( $cat->term_id, 'product_cat' );
$list[] = array($cat->term_id, $cat->name, $image, $link);
}
return $list;
}
我最近添加了:
'child_of' => 0
但是没有变化。
如何使其仅适用于顶级产品类别?
To get it works, the missing argument is just
'parent' => 0
(but not'child_of'
)
所以你的工作代码应该是这样的(并且return你的数组会正确:
function getProductCategoriesList() {
// since wordpress 4.5.0
$product_categories = get_terms( $args = array(
'taxonomy' => "product_cat",
'hide_empty' => false,
'parent' => 0,
) );
$list = array();
foreach( $product_categories as $cat ){
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$link = get_term_link( $cat->term_id, 'product_cat' );
$list[] = array($cat->term_id, $cat->name, $image, $link);
}
return $list;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效