Hey there!
If I had to assume, I would say that your charging port has a bad connection to its PCB. Before you open it up to look, however, make sure you try a different cord and/or a different wall outlet to make sure they aren't the problem. I would also check your settings to see if you have "prioritized charging" or something along those lines turned on. The setting could be making your phone not charge at certain points.
If you have confirmed that everything I've listed isn't what's wrong, I would either open it to see if the charging port is bad, or even take it to a local repair shop to have it looked at.
Best of luck!