Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想隐藏没有子类别的父名称-wordpress_Php_Wordpress_Filter_Categories - Fatal编程技术网

Php 我想隐藏没有子类别的父名称-wordpress

Php 我想隐藏没有子类别的父名称-wordpress,php,wordpress,filter,categories,Php,Wordpress,Filter,Categories,我想在这段代码中修改,以隐藏没有子类别的父名称 我试过了,但没有得到任何与此相关的信息 <?php $parent_categories = get_categories($parent_args); foreach($parent_categories as $parent_category){ ?> <?php //create main headings for other categories other than Uncategorized. if($p

我想在这段代码中修改,以隐藏没有子类别的父名称

我试过了,但没有得到任何与此相关的信息

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

        }

    }
}
?>

谢谢你的帮助,这很有魅力!,我还有一个问题,我还想限制要显示的类别数量。。我只想在这里显示4个类别,如果需要的话,我可以根据我的要求增加它……你能告诉我,当点击复选框时,我如何放置ajax加载程序图标,直到加载时,我想显示一个加载程序图像。@ShahnawazKhan你可以使用jquery,看看这段代码中的这个问题,所有未发布的帖子都显示得太多了,我不想让未发布的帖子显示在这段代码中。。。