The president is not a member of the military, and not subject to military rules of order. Technically speaking, he has the same rights as any private individual in the US to publicly express personal opinions (within the boundaries of slander and libel laws). In fact, the president has a distinct role in setting US policy and making political decisions that requires him to use and convey his own judgement. Most US presidents have spent much of their administration using cultured and curated expressions of their own opinions to craft public consensus and motivate political action. The Bully Pulpit was a long presidential tradition even before Teddy Roosevelt coined the term.
That being said, the traditional expectation has been that US presidents will express themselves in a cultured and curated fashion, with the goal of building consensus rather than spreading discord. The constitution specifies the president must be 35 years old specifically because the founders felt that younger people would be too intemperate: too aggressive, too hasty, too combative, too ideological, etc. It was their impression that by 35 a person develops a certain calm, sober, critical perspective — not being given to flights of fancy or strong egoic reactions — and that the electoral process would tend to select the most high functioning of that 'mature' group. Historically this has been the case, more or less, and while past presidents may have varied greatly on intelligence, articulateness, and political leanings, all have generally managed to separate their role as president from their role as private citizen, drawing a strict line between personal matters (likes, dislikes, squabbles and friendships) and those opinions needed for giving direction to public policy.
Unfortunately, Trump has not conformed to this tradition. He does not seem to make any distinction between public and private affairs, often airing his private grievances as though they were matters of public concern, or turning matters of public concern into personal issues of his own success or the failures of others. He is notably intemperate, and reacts from the gut rather than any sober consideration of facts and contexts. He attacks the media because — unlike his predecessors — he does not try to build consensus around policy; he demands that others agree with his opinions, and punishes those who disagree as disloyal.
Breaking with traditional institutions is not a criminal offense, obviously. But that doesn't make it any less destructive to the nation as a whole.