Can the WordPress Archives widget be customised to show only posts from previous X months?

I’ve followed the instructions on the official documentation for the wp_get_archives filter to make the widget show posts for the previous twelve months

However it doesn’t work in the way I need it to if some months have no posts.

For example, if I have posts in July, June, May, April, November, and October (ie nothing in the months between April and November) then setting the ‘limit’ parameter in the wp_get_archives filter to 6 makes the widget show this list of months:

– July

– June

– May

– April

– November

– October

But I don’t want to show November and October because they are not within the previous 6 months.

Is there a way of controlling the widget to do what I need it to? Or is a custom widget the only way to go here?

Any help would be greatly appreciated.

Many thanks,