doupo1865 2013-05-10 20:17
浏览 27
已采纳

在H3中显示类别,然后在列表中显示子类别

Ok. I tried the code from another question:

<?php
    $subcategories = get_categories('&child_of=4&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
        echo '<ul>';
        foreach ($subcategories as $subcategory) {
        echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
     }
     echo '</ul>';
?>

But didn't worked. Since I'll display the main categories in a H3, I did in a non-dynamic way. So yeah, It will look a LOT of PHP. And also: it's not working.

<h3>Automação</h3>
                <?php
                    $subcategories = get_categories('&child_of=13&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>
                <h3>Balanças</h3>
                <?php
                    $subcategories = get_categories('&child_of=34&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>              
                <h3>Caixas Registradoras</h3>
                <?php
                    $subcategories = get_categories('&child_of=42&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>
                <h3>Gavetas de Dinheiro</h3>
                <?php
                    $subcategories = get_categories('&child_of=45&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>
                <h3>Impressoras</h3>
                <?php
                    $subcategories = get_categories('&child_of=49&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>
                <h3>Informática</h3>
                <?php
                    $subcategories = get_categories('&child_of=57&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>
                <h3>Leitores</h3>
                <?php
                    $subcategories = get_categories('&child_of=61&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>
                <h3>Marcas</h3>
                <?php
                    $subcategories = get_categories('&child_of=70&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
                    echo '<ul>';
                    foreach ($subcategories as $subcategory) {
                      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
                    }
                    echo '</ul>';
                ?>

There is any way to display the main category in a H3 and the sub-categories in a one-way PHP? Because I know the problems of performance and security when I place A LOT of PHP.

Example:

H3> AUTOMACAO
UL
LI> SUB-CATEGORY
LI> SUB-CATEGORY
  • 写回答

1条回答 默认 最新

  • dongzanghua8422 2013-05-11 00:57
    关注

    I believe I got what you are looking for. Basically what I created was obtaining a list of all category IDs and filtering the parent categories in an array then going through and displaying it based on your formatting.

    <?php //Get list of parent category IDs and put it inside of $parent_categories array
        $category_ids = get_all_category_ids();
        $parent_categories = array(); 
        foreach($category_ids as $cat_id) { 
            $category = get_category($cat_id);
            if ($category->parent == 0) {
                $parent_categories[] = $category->cat_ID;
            }
        }
    ?>
    <?php //Display each parent <h3>category</h3> and <ul>subcategories</ul>
        foreach ($parent_categories as $pcat) :
            $category = get_category($pcat);
    ?>
            <h3><?php echo ucfirst($category->name); ?> </h3>
            <?php /*  if you want to show the full link you can use this instead
            <?php 
            $parentArgs =   array(
                'title_li'          => '',
                'include'           => $pcat
            );
            ?>
            <h3><?php wp_list_categories($parentArgs); ?></h3>
    
            */ ?>
            <ul>
            <?php 
                $childArgs =    array(
                    'title_li'          => '',  //You could put the parent category title in here if you want
                    'child_of'          =>$category->cat_ID
                );
                wp_list_categories($childArgs);
            ?>
            </ul>
    <?php endforeach; ?>
    

    ADDITIONAL INFORMATION REQUESTED:

    You could display based on the taxonomy here is an example.

    <?php //Get list of parent category IDs and put it inside of $parent_categories array
        $category_ids = get_all_category_ids();
        $parent_categories = array(); 
        foreach($category_ids as $cat_id) { 
            $category = get_category($cat_id);
            if ($category->parent == 0) {
                $parent_categories[] = $category->cat_ID;
            }
        }
    ?>
    <?php //Display each parent <h3>category</h3> and <ul>subcategories</ul>
        foreach ($parent_categories as $pcat) :
            $category = get_category($pcat);
            if ($category->taxonomy == 'category') : //You change the 'category' to your taxonomy.
    ?>
                <h3><?php echo ucfirst($category->name); ?> </h3>
                <ul>
                <?php 
                    $childArgs =    array(
                        'title_li'  => '',  //You could put the parent category title in here if you want
                        'child_of'  =>$category->cat_ID
                    );
                    wp_list_categories($childArgs);
                ?>
                </ul>
        <?php endif; ?>
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题