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

时间:2015-09-10 22:21:19

标签: wordpress foreach custom-post-type taxonomy custom-taxonomy

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



- 儿童

- - 孙子

- - 孙子

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


- 孩子

- - 孙子

- - 孙子

我在下面使用的代码是我可以得到的,但是在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 :(得分:0)

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