How to wrap subsection items in slide headline in beamer?

My navigation bar is divided into 2 parts, which I have sections in one part and subsections in the other part. The number of sections and subsections is too much to fit in one line. I want them to be wrapped. I found the following code in a question answered some years ago, but it is written for wrapping sections. What change should I make on the code so that it works for subsections too?

Here is the code:

\makeatletter
\hbox to #1{{%
\def\slideentry##1##2##3##4##5##6{}%
#2\hskip.3cm%
\newdimen\tmp@parbox%
\setlength\tmp@parbox{#1}%