在特定的自定义分类上显示多列类别

时间:2014-05-20 17:35:12

标签: php html wordpress

我正在尝试列出自定义分类中的所有术语,但我想至少将它们分为3列,包括他们的孩子,以实现视觉平衡。这是我到目前为止所做的。在达到最大值后,我一直坚持创建循环。在我拥有的东西上,它用'ul'包装所有后续项目,而不是创建第二个ul并列出下一批。在达到x的期限后,它应该创建另一个'ul'元素,列出其中的类别。总共有3列。

    <?php 

        $get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
        // Split into array items

        $cat_array = explode('</li>',$get_cats);
        // Amount of categories (count of items in array)
        $results_total = count($cat_array);
        // How many categories to show per list (round up total divided by 3)
        $cats_per_list = ceil($results_total / 3);
        // Counter number for tagging onto each list
        $list_number = 1;
        // Set the category result counter to zero
        $result_number = 0;
        ?>

        <?php echo $cats_per_list ; ?>

        <ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">

        <?php
        foreach($cat_array as $category) {

            $result_number++;

            if($result_number >= $cats_per_list) {
                $list_number++;
                echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
            }
            else {
                echo $category.'</li>';
            }

        }
        ?>
        </ul>           

1 个答案:

答案 0 :(得分:1)

代码非常错误。只是几个观察结果:

中的下一步
if($result_number >= $cats_per_list) {

阻止你必须将result_number重置为0,因为计数重新开始。您的当前代码只会满足该条件,因为$ cats_per_list被定义为总金额的平均值。之后它会继续计数,并且总是&gt; = $ cats_per_list

下一步:它的狡辩,但你可能不需要因为你使用的是&gt; =而需要对结果进行细化,因为1.5的操作几乎完全相同,因为1.5符合&gt; =的标准例如,1。

试试这个,看看它是否更好:

   <?php 

        $get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
        // Split into array items

        $cat_array = explode('</li>',$get_cats);
        // Amount of categories (count of items in array)
        $results_total = count($cat_array);
        // How many categories to show per list (round up total divided by 3)
        $cats_per_list = ceil($results_total / 3);
        // Counter number for tagging onto each list
        $list_number = 1;
        // Set the category result counter to zero
        $result_number = 0;
        ?>

        <?php echo $cats_per_list ; ?>

        <ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">

        <?php
        foreach($cat_array as $category) {

            $result_number++;

            if($result_number >= $cats_per_list) {
$result_number = 0;
                $list_number++;
                echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
            }
            else {
                echo $category.'</li>';
            }

        }
        ?>
        </ul> 
相关问题