Display children only when clicking the term term

0

I have a sidebar and I need to list the terms of the 'Category' taxonomy.

So far so good, what I can not do is: When you click on a term, you must list its children terms in a submenu containing a link to the listing of the child's products.

Current code

sidebar.php

<aside class="sidebar col-md-4 col-lg-3">
    <div class="caterias-sidebar">
        <p class="titulo-cat">CATEGORIA</p>
        <?php print_produtos_por_tax('Categoria'); ?>
    </div>
</aside>

UPDATED 1.1 - > functions.php

function print_produtos_por_tax($termos){

 $taxonomyName = $termos;
$terms = get_terms($taxonomyName,array('parent' => 0));
echo '<ul>';
foreach($terms as $term) {
    echo '<ul><li><a class="elementos-cat" href="#'.$term->term_id.'">'.$term->name.'</a>';
    $term_children = get_term_children($term->term_id,$taxonomyName);
    echo '<li><ul class="ul-submenu">';
    foreach($term_children as $term_child_id) {
        $term_child = get_term_by('id',$term_child_id,$taxonomyName);
        echo '<li id="#'.$term->term_id.'" class="elementos-cat li-submenu" ><a href="' . get_term_link( $term_child->term_id, $taxonomyName ) . '">' . $term_child->name . '</a></li>';
    }
    echo '</ul>';
}
echo '</ul>';
}




jQuery(".elementos-cat").click(function () {
    jQuery(".ul-submenu").slideToggle('slow');
});

You are currently not identifying which Parent has been clicked to show only their children.

It should work like this: When you click Compressor, you should list all product images independent of the Compressor subcategory, but you should open a Compressor submenu containing the list (Compressor type 1, Compressor type 2, Compressor type 3 ...) p>

Site: http://104.131.64.90

    
asked by anonymous 29.10.2014 / 23:47

1 answer

1

SOLUTION:

function print_produtos_e_filhos( $tax_name ) {
    $cats = get_terms( $tax_name, [ 'parent' => 0, 'hide_empty' => true ] );
    echo '<ul>';
    foreach ( $cats as $cat ):
        if ( ! $cat->slug != 'destaque' ) {
            ?>
            <li>
                <span class="elementos-cat li-span">
                    <a href="<?php echo get_term_link( $cat ); ?>">
                        <?php echo $cat->name; ?>
                    </a>
                </span>
                <span class="ver-filhos">\/</span>
                <?php render_children( $cat ); ?>
            </li>

        <?php
        }
    endforeach;
    echo '</ul>';
}

function render_children( $parent ) {
    $children = get_terms( $parent->taxonomy, [ 'parent' => $parent->term_id, 'hide_empty' => true ] );
    echo '<ul class="ul-submenu">';
    foreach ( $children as $kid ):
        ?>
        <li class="elementos-cat li-submenu">
            <a href="<?php echo get_term_link( $kid ); ?>">
                <?php echo $kid->name; ?>
            </a>
        </li>
    <?php
    endforeach;
    echo '</ul>';
}

jQuery:

jQuery.noConflict();

jQuery(".ver-filhos").click(function () {
    jQuery('.ul-submenu', jQuery(this).parent()).slideToggle('fast', function () {
        jQuery(this).parent().toggleClass('ul-menu-ativo');
    });
    return false;

});
    
03.11.2014 / 18:13