Code Optimisation?

Hi all, I posted this in gitter but it may of been lost in all the chatter so reposting on forum.

Im wondering if my “latest updates” code can be optimised or if its the best way of getting what I want.

On my homepage I am running this theirs 5 sections to the site with 2 sections having 120 pages and another having about 300. The other 3 sections are larger with two having about 1200 pages and the 5th section 4000 pages.

Below is the code for a section (the other 4 sections use similar code just paths are different (and more loops) is basically how I am getting “the latest section updates” to pull through onto the website.

Not sure if its optimised code or the wrong way of doing it and causing extra load on server not to compile.

The one issue I have noticed is that the loops seem to be ouputting blank tabs for all folders it does’nt need. This resu lts in the homepage being about 800kb when it should probably be about 20kb… Seems to output blank spaces on a single line for the amount of folders in that section.

Any help would be appreciated.