0

I'm working on tweaking a template which has a filterable masonry layout. I made a taxonomy-categories.php and for the filtering I need the terms of the active taxonomy. The template which I'm using has this code inside it

<ul id="filter">
                <li><a href="#" class="current" data-filter="*" title="">*</a></li>
                <?php
                $categories = get_terms('categories');
                foreach( (array)$categories as $categorie){
                    $cat_name = $categorie->name;
                    $cat_slug = $categorie->slug;
                ?>
                <li><a href="#" data-filter=".<?php echo esc_attr($cat_slug); ?>"><?php echo esc_attr($cat_name); ?></a></li>
                <?php } ?>
            </ul>

As you can see it takes all the terms from the whole taxonomy. I want it to take only the active's taxonomy sub-terms.

So if I am at URL example.com/portfolio/name_of_cat_one I want to list the children of that category.

I know that I can do that with get_term_by and tweaking a little to achieve my target, but after trying for many hours without success I ended up asking here :)

0

Well I did it..

        <ul id="filter">
                <li><a href="#" class="current" data-filter="*" title="">*</a></li>
                <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
                <?php
                $term_id = $term->term_id;
                $taxonomy_name = 'categories';
                $termchildren = get_term_children( $term_id, $taxonomy_name );

                foreach ( $termchildren as $child ) {
                    $term = get_term_by( 'id', $child, $taxonomy_name );
                    echo '<li><a href="#" data-filter=".' . $term->slug . '">' . $term->name . '</a></li>';
                }
                ?>

            </ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.