Want to create a sleek accordion menu that expands on hover? Check out my latest blog post for a step-by-step guide—perfect for WooCommerce and WordPress sites!
To create an accordion menu with hover functionality for categories and subcategories in WordPress, follow these steps:
Step 1: Plan the Menu Structure
Ensure your product categories and subcategories are properly organized in WordPress. You can do this in Products > Categories.
- Parent categories should contain the top-level categories.
- Child categories should be nested under their respective parent categories.
Step 2: Add Custom Code for Accordion Menu
You’ll need to customize your WordPress theme by adding code to your theme’s functions.php and creating CSS/JavaScript for the menu functionality.
1. Fetch Categories with Product Counts
Add the following code to your theme's functions.php
file to generate the category structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | function get_product_categories_with_count() { $args = array ( 'taxonomy' => 'product_cat' , 'orderby' => 'name' , 'hide_empty' => false, 'parent' => 0, // Fetch only parent categories ); $product_categories = get_terms( $args ); $output = '<ul class="accordion-menu">' ; foreach ( $product_categories as $category ) { $output .= '<li class="parent-category">' ; $output .= '<a href="' . get_term_link( $category ) . '">' . $category ->name . ' (' . $category -> count . ')</a>' ; $output .= get_child_categories( $category ->term_id); $output .= '</li>' ; } $output .= '</ul>' ; return $output ; } function get_child_categories( $parent_id ) { $args = array ( 'taxonomy' => 'product_cat' , 'orderby' => 'name' , 'hide_empty' => false, 'parent' => $parent_id , // Fetch child categories ); $child_categories = get_terms( $args ); if (! empty ( $child_categories )) { $output = '<ul class="subcategories">' ; foreach ( $child_categories as $child ) { $output .= '<li class="child-category">' ; $output .= '<a href="' . get_term_link( $child ) . '">' . $child ->name . ' (' . $child -> count . ')</a>' ; $output .= '</li>' ; } $output .= '</ul>' ; return $output ; } return '' ; } |
2. Add the Menu to Your Template
Paste the following code wherever you want the menu to appear, such as in a sidebar or custom page template:
1 2 | echo get_product_categories_with_count(); |
3. Style the Accordion Menu
Add this CSS to your theme’s stylesheet or via the Additional CSS section in WordPress:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | .accordion-menu { list-style : none ; padding : 0 ; margin : 0 ; } .accordion-menu > li { position : relative ; padding : 10px 15px ; border-bottom : 1px solid #ccc ; } .accordion-menu a { text-decoration : none ; color : #333 ; font-weight : bold ; display : block ; } .accordion-menu .subcategories { list-style : none ; padding : 0 15px ; margin : 0 ; display : none ; /* Initially hidden */ } .accordion-menu .subcategories li { padding : 5px 0 ; } .accordion-menu li:hover > .subcategories { display : block ; /* Show on hover */ } |
4. Add Optional JavaScript (for Smooth Transitions)
To add smooth expand/collapse transitions, enqueue a small JavaScript snippet in your theme:
1 2 3 4 | function enqueue_accordion_scripts() { wp_enqueue_script( 'accordion-menu' , get_template_directory_uri() . '/js/accordion-menu.js' , array( 'jquery' ), null, true); } add_action( 'wp_enqueue_scripts' , 'enqueue_accordion_scripts' ); |
In your theme directory, create a file js/accordion-menu.js
with the following content:
1 2 3 4 5 6 7 8 9 10 | jQuery(document).ready( function ($) { $( '.accordion-menu > li' ).hover( function () { $( this ).find( '.subcategories' ).stop( true , true ).slideDown(); }, function () { $( this ).find( '.subcategories' ).stop( true , true ).slideUp(); } ); }); |
Step 3: Test and Adjust
- Test the menu in various browsers to ensure compatibility.
- Adjust CSS styling as needed to fit your site’s design.
This implementation allows you to display product counts and reveal subcategories on hover, creating a dynamic accordion-style menu. Let me know if you need help refining it!
No comments:
Post a Comment