I am using a Product Categories List Block to list all product categories in sidebar in shop and category pages (like filters). I am listing categories in my site by using a wordpress Widget. Categories are shown but there is no active class for current category or ancestor category. How to make it that if user is on current category, the li item will get current-cat class and ancestor current-ancestor class?
2
Answers
I needed to solve this myself, so here is my current solution.
Since there is currently no filter available for the default WooCommerce blocks, I solved it using PHP’s DOMDocument parser while rendering the block.
You can add a filter to
render_block
to catch the html for the specific block, in this case the block name iswoocommerce/product-categories
. By adding checks for non-admin & non-json requests we make sure the changes only happen on the front-end output.If you add the code below to your theme’s
functions.php
, a classcurrent-category-item
is added to the list item that matches the current url.I also added a header to the block, but these lines are optional as long as you
return $dom->saveHTML()
after your changes.it works very well, but I edited this line
to
my site is in Cyrillic and it breaks the encoding