0

i want modification in this code to hide parent name which does not have child category.

I have tried but didn't got anything related to this.

 <?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++;
        }
        }

}
} ?>

1 Answers1

0

Before printing anything you should first fetch all child category, Then you cant check child item is empty or not with empty function and then continue the current loop to skip this category, So you can add if(empty($firstchild_categories)) continue and your could can be like this

<?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>";

        }

    }
}
?>
Ali Akbar Azizi
  • 2,656
  • 2
  • 21
  • 34
  • Thanks for the help this works like a charm!, i have one more question i also wanted to limit the number of categories to be displayed.. i just want to show 4 categories here, and if needed i can increase it as per my requirement... – Shahnawaz Khan Sep 12 '19 at 05:30
  • Could you please tell me how can i put ajax loader icon when the chechbox gets clicked and till time when it loads i want to display one loader image. – Shahnawaz Khan Sep 16 '19 at 19:25
  • @ShahnawazKhan You can use jquery for this, look at this question https://stackoverflow.com/q/68485/1827594 – Ali Akbar Azizi Sep 17 '19 at 06:22
  • In this code all posts which are not published are too displaying i don't want unpublished posts to be displayed in this... – Shahnawaz Khan Sep 26 '19 at 14:33