我建立一個房地產網站,所以我有很多類別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因爲 秒ondsub1也是孩子cat1但我只想sub1我怎麼能做到這一點?任何建議
感謝回答我只是忘了我不應該使用child_of :) – RItika