The photon is an elementary particle in the standard model of particle physics. It does not have a wavelength. It is characterized in the table as a point particle with mass zero and spin one. Its energy is given by $E=h\nu$, where $\nu$ is the frequency of the classical electromagnetic wave which can be built up by photons of the same energy.
This is where the confusion comes. The wavelength and frequency characterize the emergent electromagnetic wave from very many photons. How the classical wave emerges can be seen here although it needs a quantum field theory background to understand it. The photon, as a quantum mechanical entity, has a quantum mechanical wavefunction. This wavefunction complex conjugate squared gives the probability density for the specific photon to be at $(x,y,z,t)$. The frequency in the wavefunction is the frequency of the possible emergent classical wave, but for the individual photon it is only connected with probability of manifestation, as for example in the single photon double slit experiments.
single-photon camera recording of photons from a double slit illuminated by very weak laser light. Left to right: single frame, superposition of 200, 1’000, and 500’000 frames
you ask:
"How can a photon have a wavelength of a few kilometres and yet still be thought of as a particle?
It does not. It takes zillions of photons to build up the classical electromagnetic wave. In the photos above each individual photon gives a little dot. The build up gives the probability density distribution for photons, and lo, there is a frequency associated with the interference pattern, even though the photon manifests individually as a dot at the $(x,y)$ of the screen.
That is why we need quantum mechanics.
Edit after this question became the main duplicate of another one, where I have a long answer/comment that might be of interest to readers.
Does a single photon have a wavelength or not? [duplicate]