0

Currently this code is displaying all records for all taxonomies but I only want to display the records for the specific taxonomy that the user clicks into, so I think I need to filter by slug or get the id and filter by that but not sure how.

<?php 
$categories    = get_terms('work-type', array(
    'hide_empty' => 0
));
$count         = 1;
$count2        = 1;
$subcategories = $subsubcategories = $categories;
echo '<ul class="tabs">';
foreach ($categories as $category) {
    // Only top level terms
    if (0 != $category->parent) {
        continue;
    }
    $active = $category->term_id === 39 ? 'current' : '';
    echo '<li class="tab-link ' . $active . '" data-tab="tab-' . $count++ . '">' . $category->name . '</li>';
}
echo '</ul>';
foreach ($categories as $category) {
    // Only top level terms
    if (0 != $category->parent) {
        continue;
    }
    $active = $category->term_id === 39 ? 'current' : '';
    echo '<div id="tab-' . $count2++ . '" class="tab-content ' . $active . '">';
    echo '<ul class="wsul-parent">';
    foreach ($subcategories as $subcategory) {
        // Only child terms
        if ($category->term_id != $subcategory->parent) {
            continue;
        }
        $args1  = array(
            'post_type' => 'permanent',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'work-type',
                    'field' => 'term_id',
                    'terms' => array(
                        $subcategory->term_id
                    )
                )
            )
        );
        $query1 = new WP_Query($args1);
        // It is second level, display it
        echo '<li class="wsli-parent">' . $subcategory->name . ':';
        echo '<ul class="wsul-child">';
        foreach ($subsubcategories as $subsubcategory) {
            $args2  = array(
                'post_type' => 'permanent',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'work-type',
                        'field' => 'term_id',
                        'terms' => array(
                            $subsubcategory->term_id
                        )
                    )
                )
            );
            $query2 = new WP_Query($args2);
            // Only child terms
            if ($subcategory->term_id != $subsubcategory->parent) {
                continue;
            }
            // It is third level, display it
            echo '<li class="wsli-child">' . $subsubcategory->name . '</li>';
            echo '<ul class="wsul-gchild">';
            while ($query2->have_posts()) {
                $query2->the_post();
                echo '<li class="wsli-gchild">';
                echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
                echo '</li>';
            }
            echo '</ul>';
        }
        if ($category->term_id != 39) {
            echo '<ul class="wsul__child">';
            while ($query1->have_posts()) {
                $query1->the_post();
                echo '<li class="wsil__child">';
                echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
                echo '</li>';
            }
            echo '</ul>';
        }
        wp_reset_postdata();
        echo '</ul>';
        echo '</li>';
    }
    echo '</ul>';
    echo '</div>';
}
?>

Your Answer

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

Browse other questions tagged or ask your own question.