【发布时间】:2017-03-24 05:01:43
【问题描述】:
我正在建立一个房地产网站,所以我有很多 3 到 4 类 级别子类别,但我的要求是只显示一个级别子 父类别的类别,例如 sub1 ,sub2 和 sub3 只看到我拥有的结构这样的类别
- cat1
- sub1
- secondsub1
- cat2
- sub2
- secondsub2
- cat3
- sub3
- secondsub3
$categories = get_terms(
'category',
array('hide_empty' => 0,'parent' => 0,'number' =>3,'order'=> 'ASC')
);
foreach ($categories as $category) {
$catName= $category->term_id;
<?php echo get_cat_name( $catName ); // this is the main category ?>
<!-- subcategory code starts here-->
$args = array(
'type' => 'post',
'child_of' => $catName,
'parent' => get_query_var(''),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '5',
'taxonomy' => 'category',
'pad_counts' => true );
$categories = get_categories($args);
foreach($categories as $category) {
<a href="<?php echo get_category_link( $category->term_id )?>" title="View posts in <?php echo $category->name?>"><?php $category->name;?></span><!--these are the sub category-->
}
}
结果我得到了 cate1 sub1->secondsub1 因为 secondsub1 也是 cat1 的孩子,但我只想要 sub1 我怎么能做到这一点?任何建议
【问题讨论】:
标签: php wordpress categories