如何只获取父类别ID。不是儿童类别ids 我尝试了这段代码,之前向我展示了所有类别的ID
<?php $category_ids = get_all_category_ids();
foreach($category_ids as $cat_id) {
$cat_name = get_cat_name($cat_id);
//echo '<span class="png_bg category_icon"></span>' . $cat_name ;
?>
<option><?php echo '<span class="png_bg category_icon"></span>' . $cat_name ; ?></option>
<?php
}
?>
</select>
答案 0 :(得分:1)
可以通过多种方式完成。其中之一是
$categories = get_categories();
foreach ($categories as $cat)
{
// if it is a topmost category , it has no parents, ie parent=0
if($cat->parent < 1)
{
echo $cat->category_nicename
echo $cat->cat_name ;
}
}
答案 1 :(得分:0)