dongli564510 2014-05-20 17:35
浏览 25

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

I'm trying to list all terms on a custom taxonomy but I wanted to at least group them into 3 columns including their children to have a visual balance. Here's what I've done so far. I'm stuck on creating the loop after it reached the maximum term. On what I have, It wrapped all the succeeding items with 'ul' instead of creating a second ul and list the next batch. After it reaches the x amount of term it should create another 'ul' element listing categories in it. There will be a total of 3 columns.

    <?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条回答 默认 最新

  • dongyan4157 2014-05-20 18:05
    关注

    The code is very buggy. Just a couple of observations:

    Next in the

    if($result_number >= $cats_per_list) {
    

    Block you are going to have to reset result_number to 0 since the count starts over again. Your current code would only meet that condition once since $cats_per_list is defined as the average of the total amount. After that it would continue counting up and ALWAYS be >= $cats_per_list

    Next: it's quibble but you probably don't need to ceil the result since you are using >=, that operation pretty much does the same thing since 1.5 will meet the criteria of >= 1 as a for instance.

    Try this and see if it is any better:

       <?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> 
    
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?