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

时间:2013-05-10 20:17:41

标签: php wordpress

确定。我尝试了另一个问题的代码:

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

但没有奏效。由于我将在H3中显示主要类别,因此我采用了非动态方式。所以是的,它会看起来很多PHP。而且:它不起作用。

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

有没有办法在H3中显示主要类别,在单向PHP中显示子类别?因为当我放置很多PHP时,我知道性能和安全性的问题。

示例:

H3> AUTOMACAO
UL
LI> SUB-CATEGORY
LI> SUB-CATEGORY

1 个答案:

答案 0 :(得分:2)

我相信我得到了你想要的东西。基本上我创建的是获取所有类别ID的列表并过滤数组中的父类别,然后根据您的格式进行显示。

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

要求的其他信息:

您可以根据分类法显示这里的示例。

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