我只想显示父类别名称,而不显示子类别,也不需要在任何产品或帖子中使用,我只需要列出父级自定义类别即可。
我有尝试get_terms,wp_list_categories函数,但它也显示孩子
这是我的代码。
<?php
require_once('connection.php');
$taxonomy = 'product_cat';
$orderby = 'parent';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 0; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'childless' => false,
'child_of' => 0,
'title_li' => $title,
'hide_empty' => $empty,
'hierarchical'=>1
//'hierarchical=0&depth=1'
);
$rrr=wp_list_categories( $args );
print_r($rrr);
?>
它也显示孩子,但我只需要父类别名称。
我用的Product_cat是woocommerce类别,当我将它与get_terms一起使用时,给出的是空数组。
我也使用这种方式,但product_cat无法与get_terms一起使用
<?php
$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('product_cat',$parent_cat_arg);
foreach ($parent_cat as $catVal) {
/*some code*/
}
?>
另请参阅附件的图像,我已经解释了我所需要的。
试试这个套装parent
是0
$taxonomy = 'product_cat';
$orderby = 'parent';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 0; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'childless' => 0,
'child_of' => 0,
'title_li' => $title,
'hide_empty' => $empty,
'parent'=>0,
);
$rrr=wp_list_categories( $args );
print_r($rrr);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句