View children (Children) just by clicking on parent term

Asked

Viewed 786 times

0

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

So far so good, what I can’t do is: By clicking on a term, you should list his children’s terms in a submenu containing a link to the product listing of that child’s.

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');
});

Currently not identifying which Father was clicked to show only his children.

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

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Website: http://104.131.64.90

  • 2

    Don’t use the snippet Javascript to format PHP code. And see also: http://meta.pt.stackoverflow.com/questions/297/quando-se-deve-coloca-o-nome-da-linguagem-no-t%C3%Adtulo

  • 1

    Let me get this straight. In this page for example: "http://104.131.64.90/categoria/compressor/" the sidebar would have to show the "children" of compressors?

  • 1

    I’ll leave a hint for anyone who doesn’t know about it. From the excerpt of your code it looks like you are doing a function for every thing on your site. Take care! A site like this has several pages with different functions. To do this use Object Orientation, you create a consistent and human code. Links: Introduction to OOP - Devmedia and A little about classes - Slideshare

  • Please publish the solution in response form. And if I explain what you’ve done to solve it will help anyone looking for the same thing and ends up here.

  • If you want to publish your reply on your behalf, let me know and I’ll delete mine that I put in wiki mode. The site is Questions & Answers, solutions should not be posted within the question. Thank you.

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;

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.