0
votes

I have a taxonomy structure thats 3 levels deep, with the name 'services_tax':

Parent

- Child

- - Grandchild

- - Grandchild

I am trying to display all taxonomies associated with the current custom post, and echo them out as column divs (thirds), not showing the first parent like so:


- Child

- - Grandchild

- - Grandchild

The code im using below is as far as I can get however, using get_terms inside of the get_the_terms foreach loop is grabbing terms that are not associated with the post.

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

I have gone through as many Stackoverflow questions/answers as I could find with no luck

This post being the most helpful.

If I could only use get_the_terms and pass an argument I feel like I could figure it out. I am feeling very stuck and any help would be greatly appreciated.

1
So, you are not getting "Parent" taxonomy?Atif Tariq

1 Answers

0
votes

I guess I just figured it out, use wp_get_post_terms() instead of get_terms() in the foreach.