Just put two and two together. Riley's previous assignment in Engineering explains how Riley knew Engineering so well to be able to seal the doors, take control of ship communications and turn off the engines in The Naked Time. During the The Naked Time, he is acting as the ship's Navigator (promotion to gold shirt "Command Division" job). After The Naked Time, Kirk may have transferred Riley (after demonstrating his masterful skills over ship communications) to Communications as found later in The Conscience of the King. Why is he still wearing the gold shirt? Maybe Communication Officers are not color (division) dependent, after all, over the series we see all shirt colors sit in on the Communications Station. Memory Alpha says, "The role of communications officer (also known as comm officer) was a specialized occupation held by crewmembers aboard Starfleet vessels, installations, and bases. Found across multiple divisions, the individuals holding the position were held responsible for managing all incoming and outgoing transmissions, whether they were visual, audio, or text communications."