0

This code shows all categories available and also the subcategories beautiful listed right below its suitable parent category. Is it possible to make a nested menu, so that when I click on the parent category the subcategories are scrolling down/ dropping down right below the parent category?

$args = array(
    'child_of'            => 0,
    'current_category'    => 0,
    'depth'               => 0,
    'echo'                => 1,
    'exclude'             => '',
    'exclude_tree'        => '',
    'feed'                => '',
    'feed_image'          => '',
    'feed_type'           => '',
    'hide_empty'          => 0,
    'hide_title_if_empty' => false,
    'hierarchical'        => true,
    'order'               => 'ASC',
    'orderby'             => 'name',
    'separator'           => '<br />',
    'show_count'          => 0,
    'show_option_all'     => '',
    'show_option_none'    => __( 'No categories' ),
    'style'               => 'list',
    'taxonomy'            => 'category',
    'title_li'            => __( 'Categories' ),
    'use_desc_for_title'  => 1,
);

$list_categories = get_categories( $args );
echo "<div class='container'>";
echo "<div class='productsubmenu'>";
echo "<ul>";

foreach( $list_categories as $cat ) {

    $cat_id = $cat->term_id;
    $args2  = array(
        'child_of'            => $cat_id,
        'current_category'    => 0,
        'depth'               => 0,
        'echo'                => 1,
        'exclude'             => '',
        'exclude_tree'        => '',
        'feed'                => '',
        'feed_image'          => '',
        'feed_type'           => '',
        'hide_empty'          => 0,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'ASC',
        'orderby'             => 'name',
        'separator'           => '<br />',
        'show_count'          => 0,
        'show_option_all'     => '',
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => __( 'Categories' ),
        'use_desc_for_title'  => 1,
    );


    if( $cat->category_parent == 0 ) {

        $category_id = $cat->term_id;

        echo '<li class="parentss"><a href="'.get_term_link( $cat ) . '">' . $cat->name . '</a>';


        $subcats = get_categories( $args2 );
        echo '<ul class="wooc_sclist">';

        foreach( $subcats as $sc ) {

            $link = get_term_link( $sc->slug, $sc->taxonomy );
            echo '<li class="" ><a href="'. $link .'">'.$sc->name.'</a></li>';
            var_dump( cat_is_ancestor_of($cat_id,32));
            var_dump( $cat_id);

        }

    }

    echo '</ul>';
    echo "</li>";

}
1

You don't need to create category listing functionality, WordPress already created a helper function to handle this easily. Take a look at wp_list_categories(). And what you need can be achieved through some CSS and a little bit of JS if needed. You'll find lots of jQuery menu plugin you can use any of those or if you need a tree-like structure then check this https://www.jstree.com/

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.