Limit number of subcategories shown
-
My client has 1000 subcategories i one parent category, and they all show (making the page load slooow), even though I have set 4 products per row, and 10 rows in Appearance settings. Should be only 40. But it’s not working on subcategories view; only products. How can I limit subcategories?
-
Hi
To limit the display of subcategories in WooCommerce and improve the loading speed of your page.
Change the number of products per row and rows per page for subcategories: While the Appearance settings in WooCommerce control the number of products per row and rows per page, they might not directly apply to subcategories. To address this, you can modify the code in your theme’s functions.php file or use a custom plugin. Here’s an example of how you can limit the subcategories to 4 per row and 10 rows per page:
// Modify subcategory product counts add_filter('woocommerce_subcategory_count_html', 'custom_subcategory_count_html'); function custom_subcategory_count_html($html) { // Change the number of subcategories per row and rows per page as desired $subcategory_limit = 4; $subcategory_rows = 10; return preg_replace('/<\/a>\s*<span.*?>\s*\((\d+)\)<\/span>/', '</a>', $html, $subcategory_limit * $subcategory_rows); }
Thanks
Ahir HemantHi there @katrineds ??
How did it go with the suggested approach from @hemant-ahir ?
My client has 1000 subcategories i one parent category
Furthermore, could you elaborate further on the workflow of a customer on said website? How is navigation and product discovery implemented?
Hi,
Thanks for suggestion.
I haven’t tested it yet. Waiting for approval from the client, to limit the number of subcategories shown per page.Furthermore, could you elaborate further on the workflow of a customer on said website? How is navigation and product discovery implemented?
There’s a parent category with about 1000 subcategories. In these subcategories there’s between 1 and 15 new subcategories and THEN there are products in these.
Update: Tried the code; didn’t work. All subcategories are still visible on page load.
Hi @katrineds
The settings you mentioned – 4 products per row and 10 rows – apply specifically to products, not subcategories. When you’re viewing a category page, WooCommerce will, by default, display all subcategories. This is why you are seeing all 1000 subcategories and why page load times might be slower than expected.
I understand that the code shared by @hemant-ahir didn’t work as expected, and all subcategories are still visible on the page load.
There’s a parent category with about 1000 subcategories. In these subcategories there’s between 1 and 15 new subcategories and THEN there are products in these.
It sounds like you have a complex category structure with a parent category, multiple subcategories, and further subcategories within those. Given the size and complexity of your category structure, there could be a few reasons why the code isn’t working as expected.
However, to limit the number of subcategories displayed, can you add this code snippet to your child theme’s functions.php file:
add_action( 'pre_get_posts', 'wc_custom_query' ); function wc_custom_query( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_product_category() ) { $query->set( 'posts_per_page', 40 ); return; } }
This function checks if the page being loaded is a product category page and, if so, limits the number of ‘posts’ (in this case, subcategories) displayed to 40.
Please note that you should replace ’40’ with the number of subcategories you want to display. Also, be cautious when editing the functions.php file, as errors can potentially bring down your entire site. I’d recommend making a backup of your site before proceeding.
If you’re still having trouble, I recommend hiring a developer for further assistance. Because custom coding is not something we can assist with directly.
If you have any further questions on development or custom coding, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:
- WooCommerce Developer Resources Portal
- WooCommerce Advanced Facebook group
- WooCommerce Developer Slack Channel
- Hire a WooCommerce Expert
I hope this helps! Please let me know if you have any other questions.
Thanks!
Yes, it is a very complex, deep structure. It’s what the client wants.
Unfortunately the new code didn’t work either. I will contact a developer. I know custom coding isn’t covered by support. Thought it might just be something ‘simple’ I had overlooked. Appreciate the help so far. Thanks! ??
Hi @katrineds
I’m sorry to hear that the new code didn’t work as expected. Your initiative to contact a developer is a good step forward.
Sometimes, even what appears to be ‘simple’ can turn out to be a little more complex than anticipated, especially when dealing with custom code. It’s always a good idea to have a professional developer on hand for these types of situations.
Meanwhile, I’ll be marking the thread as solved for now, but feel free to open a new one if you have any other questions.
Thank you again for your understanding and cooperation.
- The topic ‘Limit number of subcategories shown’ is closed to new replies.