首页 文章

从第一个子项开始,在三列中显示自定义分类

提问于
浏览
0

我有一个深度为3级的分类结构,名称为'services_tax':

Parent - Child - - Grandchild - - Grandchild

我试图显示与当前自定义帖子相关联的所有分类,并将它们作为列div(三分之一)回显,而不显示第一个父类,如下所示:

- Child - - Grandchild - - Grandchild

我在下面使用的代码是我可以得到的,然而,在get_the_terms foreach循环中使用get_terms是抓取与帖子无关的术语 .

<div class="three-column cf">
<?php
$myterm = 'services_tax';
$terms = get_the_terms( get_the_ID(), $myterm );
if ( $terms ) :
?>
    <?php $i = 0; ?>
    <?php foreach ( $terms as $term ) : ?>
        <?php if ( $term->parent == 0 ) : ?>
            <?php
            $subargs = array(
                'orderby' => 'name',
                'order' => 'ASC',
                'hide_empty' => true,
                'hierarchical' => true,
                'child_of' => $term->term_id,
                'parent' => $term->term_id
            );
            $children = get_terms( $myterm, $subargs );
            ?>
            <?php if( $children ) : //if it has sub-categories ?>
                <?php foreach( $children as $child ) : ?>
                    <?php if ( $i != 0 && $i % 3 == 0 ) : ?>
                        </div><!-- .three-column -->
                        <div class="three-column cf">
                    <?php endif; ?>
                    <div class="third">
                        <?php //var_dump($i); ?>
                        <h4 class="h3"><?php echo $child->name; ?></h4><!-- .h3 -->
                        <?php
                        $subsubargs = array(
                            'orderby' => 'name',
                            'order' => 'ASC',
                            'hide_empty' => true,
                            'hierarchical' => true,
                            'child_of' => $child->term_id,
                            'parent' => $child->term_id
                        );
                        $sub_children = get_terms( $myterm, $subsubargs );
                        ?>
                        <?php if( $sub_children ) : //if it has sub-sub-categories ?>
                            <ul>
                                <?php foreach( $sub_children as $sub_child ) : ?>
                                    <li><?php echo $sub_child->name; ?></li>
                                <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
                    </div><!-- .third -->
                    <?php $i++; ?>
                <?php endforeach; ?>
            <?php endif; ?>
        <?php endif; ?>
    <?php endforeach; ?>
<?php endif; ?>

我已经经历了尽可能多的Stackoverflow问题/答案,因为我没有运气

This post是最有帮助的 .

如果我只能使用get_the_terms并传递一个参数,我觉得我可以搞清楚 . 我感到非常困难,任何帮助将不胜感激 .

1 回答

  • 0

    我想我只是弄明白了,在foreach中使用wp_get_post_terms()而不是get_terms() .

相关问题