我想隐藏没有子类别的父名称 - wordpress
I want to hide parent name which does not have child categories - wordpress
我想修改此代码以隐藏没有子类别的父名称。
我试过了,但没有得到任何与此相关的信息。
<?php
$parent_categories = get_categories($parent_args);
foreach($parent_categories as $parent_category){ ?>
<?php //create main headings for other categories other than Uncategorized.
if($parent_category->name!="Uncategorized"){
$category_label = "By ";
echo '<br>';
echo '<h5>'.$category_label.''.$parent_category->name.'</h5>';
//fetch the parent category's id
$firstchild_args['parent'] = $parent_category->term_id;
$firstchild_categories = get_categories($firstchild_args);
//fetch all the first level children categories
//fetch all the first level children categories
$limit=15; // Set Child limit here
$counter=0;
foreach($firstchild_categories as $firstchild_category){
if($counter<$limit){
$output = "";
$output = $output."<div class=\"checkboxes-group\">";
$output = $output." <input type=\"checkbox\" value=".$firstchild_category->slug." class=\"js-filter-checkbox\" name=\"category[]\" id=".$firstchild_category->cat_ID.">
<span style='font-size: 17px; color: #404040;'>".$firstchild_category->name;
$output = $output."</div>";
echo $output;
$output = $output."</form>";
$counter++;
}
}
}
} ?>
在打印任何东西之前你应该首先获取所有子类别,然后你不能用empty
函数检查子项是否为空然后continue
当前循环跳过这个类别,所以你可以添加 if(empty($firstchild_categories)) continue
你可以像这样
<?php
$parent_categories = get_categories($parent_args);
$limit = 15; // Set Child limit here
$counter = 0;
foreach ($parent_categories as $parent_category) {
//create main headings for other categories other than Uncategorized.
if ($parent_category->name != "Uncategorized") {
//fetch the parent category's id
$firstchild_args['parent'] = $parent_category->term_id;
$firstchild_categories = get_categories($firstchild_args);
if( empty($firstchild_categories) )
continue;
if($counter > $limit)
break;
$counter++;
$category_label = "By ";
echo '<br>';
echo '<h5>' . $category_label . '' . $parent_category->name . '</h5>';
//fetch all the first level children categories
//fetch all the first level children categories
foreach ($firstchild_categories as $firstchild_category) {
$output = "";
$output = $output . "<div class=\"checkboxes-group\">";
$output = $output . " <input type=\"checkbox\" value=" . $firstchild_category->slug . " class=\"js-filter-checkbox\" name=\"category[]\" id=" . $firstchild_category->cat_ID . ">
<span style='font-size: 17px; color: #404040;'>" . $firstchild_category->name;
$output = $output . "</div>";
echo $output;
$output = $output . "</form>";
}
}
}
?>
我想修改此代码以隐藏没有子类别的父名称。
我试过了,但没有得到任何与此相关的信息。
<?php
$parent_categories = get_categories($parent_args);
foreach($parent_categories as $parent_category){ ?>
<?php //create main headings for other categories other than Uncategorized.
if($parent_category->name!="Uncategorized"){
$category_label = "By ";
echo '<br>';
echo '<h5>'.$category_label.''.$parent_category->name.'</h5>';
//fetch the parent category's id
$firstchild_args['parent'] = $parent_category->term_id;
$firstchild_categories = get_categories($firstchild_args);
//fetch all the first level children categories
//fetch all the first level children categories
$limit=15; // Set Child limit here
$counter=0;
foreach($firstchild_categories as $firstchild_category){
if($counter<$limit){
$output = "";
$output = $output."<div class=\"checkboxes-group\">";
$output = $output." <input type=\"checkbox\" value=".$firstchild_category->slug." class=\"js-filter-checkbox\" name=\"category[]\" id=".$firstchild_category->cat_ID.">
<span style='font-size: 17px; color: #404040;'>".$firstchild_category->name;
$output = $output."</div>";
echo $output;
$output = $output."</form>";
$counter++;
}
}
}
} ?>
在打印任何东西之前你应该首先获取所有子类别,然后你不能用empty
函数检查子项是否为空然后continue
当前循环跳过这个类别,所以你可以添加 if(empty($firstchild_categories)) continue
你可以像这样
<?php
$parent_categories = get_categories($parent_args);
$limit = 15; // Set Child limit here
$counter = 0;
foreach ($parent_categories as $parent_category) {
//create main headings for other categories other than Uncategorized.
if ($parent_category->name != "Uncategorized") {
//fetch the parent category's id
$firstchild_args['parent'] = $parent_category->term_id;
$firstchild_categories = get_categories($firstchild_args);
if( empty($firstchild_categories) )
continue;
if($counter > $limit)
break;
$counter++;
$category_label = "By ";
echo '<br>';
echo '<h5>' . $category_label . '' . $parent_category->name . '</h5>';
//fetch all the first level children categories
//fetch all the first level children categories
foreach ($firstchild_categories as $firstchild_category) {
$output = "";
$output = $output . "<div class=\"checkboxes-group\">";
$output = $output . " <input type=\"checkbox\" value=" . $firstchild_category->slug . " class=\"js-filter-checkbox\" name=\"category[]\" id=" . $firstchild_category->cat_ID . ">
<span style='font-size: 17px; color: #404040;'>" . $firstchild_category->name;
$output = $output . "</div>";
echo $output;
$output = $output . "</form>";
}
}
}
?>