【发布时间】:2016-10-24 00:08:21
【问题描述】:
我想只显示父类别名称而不是子类别名称 没有在任何产品或帖子中使用,我只需要列出父自定义类别。
我尝试了 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 它被赋予 null 数组。
我也是这样使用的,但是 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*/
}
?>
请参阅附件图片,我已经解释了我需要什么。
【问题讨论】:
-
你试过了吗:
hierarchical=0&depth=1? -
是的,我试过了。