gpt4 book ai didi

wordpress - 如何获得 Woocommerce 顶级产品类别?

转载 作者:行者123 更新时间:2023-12-05 02:12:29 25 4
gpt4 key购买 nike

我正在尝试在主页模板中检索顶级产品类别。我使用了下面的代码,但不知道为什么它不起作用。

 <?php
global $post, $product;
$taxonomy = 'product_cat'; //Choose the taxonomy
$terms = get_terms( $taxonomy ); //Get all the terms

foreach ($terms as $term) { //Cycle through terms, one at a time

// Check and see if the term is a top-level parent. If so, display it.
$parent = $term->parent;
if ( $parent=='0' ) {

$term_id = $term->term_id; //Define the term ID
$term_link = get_term_link( $term, $taxonomy ); //Get the link to the archive page for that term
$term_name = $term->name;
echo '<a class="ccats" href="' . $term_link . '"><span class="label">' . $term_name . '</span></a>';
} }
?>

请帮助我获取顶级类别。

最佳答案

您只需要在 get_terms() 函数中添加以下参数:

$terms = get_terms( array('taxonomy' => 'product_cat', 'parent' => 0) );

foreach ( $terms as $term ){
$term_link = get_term_link( $term, $taxonomy );

echo '<a class="ccats" href="'.$term_link.'"><span class="label">'.$term->name.'</span></a>';
}

这是 all available arguments可以在 get_terms() 函数中使用。


对于产品类别图片:Get and display the product category featured image in Woocommerce

关于wordpress - 如何获得 Woocommerce 顶级产品类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55951410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com