Permalink in WordPress loop outputs for each new line

This is happening because the_category() outputs a list of categories, with links. So it’s outputting its own <a> tags, and you cannot put <a> tags inside other <a> tags. It’s not valid HTML.

What you’re seeing is the browser’s attempt to produce valid HTML based on invalid markup. HTML is very resilient, and invalid markup will not cause it to crash. THe browser just does things like this. If you look at the raw HTML returned for the page in the Network tabs of the browser’s developer tools you will see what HTML your template actually created.

If you want to output the list of a post’s categories without links, you will need to do something like this: