在wordpress中显示子类别

在wordpress中显示子类别,wordpress,categories,Wordpress,Categories,下面的代码显示父类别图像 我可以在父类别图像下显示子类别吗 <?php $cat = get_query_var('cat'); $args=array('term_args'=>array('child_of'=>$cat,'hide_empty'=>0,'orderby'=>'id')); $terms = apply_filters( 'taxonomy-images-get-terms','',$args ); if ( ! empty( $terms )

下面的代码显示父类别图像 我可以在父类别图像下显示子类别吗

<?php $cat = get_query_var('cat');
$args=array('term_args'=>array('child_of'=>$cat,'hide_empty'=>0,'orderby'=>'id'));
$terms = apply_filters( 'taxonomy-images-get-terms','',$args );
if ( ! empty( $terms ) ) {
foreach ( (array) $terms as $term ) {
   echo $this_category;
   ?>
<li class="col-md-3">
 <a href="<?php echo $term->name; ?>"><?php echo wp_get_attachment_image( $term->image_id, 'detail' ) ?></a>
</li>
<?php
}
}
 ?>


  • 我制作了两个自定义函数,用于获取项目中的父子类别。 创建一个名为project helper.php的文件,然后转到functions.php,最后将创建的project helper.php文件添加到functions.php

    //project-helper.php

    /**
     * Get Category
     * @param $taxonomy
     * @param string $order
     * @return array
     */
    function getPostTermCategory($taxonomy, $order = 'asc')
    {
        $term_query = new WP_Term_Query([
            'taxonomy' => $taxonomy,
            'order' => $order,
            'hide_empty' => false,
            'parent' => 0
        ]);
        $categories = [];
        foreach ($term_query->terms as $term) {
            $categories[] = $term;
        }
        return $categories;
    }
    
    /**
     * Get Child Categories of Parent
     * @param $taxonomy
     * @param $parentId
     * @param string $order
     * @return array
     */
    function getPostTermChildCategory($taxonomy, $parentId, $order = 'asc')
    {
        $term_query = new WP_Term_Query([
            'taxonomy' => $taxonomy,
            'order' => $order,
            'hide_empty' => false,
            'parent' => $parentId
        ]);
        $categories = [];
        foreach ($term_query->terms as $term) {
            $categories[] = $term;
        }
        return $categories;
    }
    
    现在从模板中调用创建的函数

    $projectRegions = getPostTermCategory('project-region-cat');
    foreach ($projectRegions as $projectRegion) {
                        
        <?php $categoryChild = getPostTermChildCategory('project-region-cat', $projectRegion->term_id);
            foreach ($categoryChild as $categoryChild) { ?>
            <option value="<?= $categoryChild->slug; ?>"><?= $categoryChild->name; ?></option>
        <?php } ?>
    }
    
    $projectRegions=getPostTermCategory('project-region-cat');
    foreach($projectRegions作为$projectRegion){
    
    $projectRegions = getPostTermCategory('project-region-cat');
    foreach ($projectRegions as $projectRegion) {
                        
        <?php $categoryChild = getPostTermChildCategory('project-region-cat', $projectRegion->term_id);
            foreach ($categoryChild as $categoryChild) { ?>
            <option value="<?= $categoryChild->slug; ?>"><?= $categoryChild->name; ?></option>
        <?php } ?>
    }