1

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

<?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 中显示子类别?因为当我放置 A LOT PHP 时,我知道性能和安全性问题。

例子:

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

1 回答 1

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; ?>
于 2013-05-11T00:57:25.197 回答