A core–shell structured magnetic molecularly imprinted nanoparticle (MMINP) was developed, capable of forming a photonic crystal (PC) sensor in the presence of a magnetic field for the detection of sulfamethazine (SM2) residues in food. The preparation conditions were meticulously optimized, and the adsorption performance of MMINPs was comprehensively characterized through Adsorption Kinetics Experiments and Equilibrium Binding Experiments. As the concentration of SM2 varied, the structural colors were systematically characterized under a designated magnetic field strength. The experiments revealed that within the range of SM2 concentrations from 10−1 μg/L to 103 μg/L, there was a shift in the reflected wavelength from 570 to 610 nm, and the color changed from green to red–orange. The sensor achieved a minimum detection limit of 2.75 μg/L, a maximum adsorption capacity of 3.86 mg/g, an imprint factor of 1.70, and was reused for at least five adsorption-resolution cycles. The recoveries ranged from 69.97% to 102.68% and RSD < 8.73%. The sensor introduces an innovative approach for the rapid visual colorimetric detection of SM2 in complex food matrices.