Density-functional theory (DFT) calculations of the magnetic shielding for nuclear magnetic resonance (NMR) in solids provide an important contribution for understanding the experimentally observed chemical shifts. It is known that the calculated NMR shielding parameters for a particular nucleus in a series of compounds correlate well with the experimentally measured chemical shifts; however, the slope of a linear fit often differs from the ideal value of 1.0. Focusing on a series of ionic compounds (fluorides, oxides, bromides, and chlorides), we show that the error is caused by the generalized gradient approximation (GGA) to the exchange-correlation functional and it is related to the well-known band-gap problem. In order to devise an ab initio approach that would correctly reproduce the variation of the shifts within a series of compounds, we test various DFT based approaches. A simple GGA + U scheme with the orbital field acting on the cation d states does not work in a general way. Also, the popular hybrid functionals (including the screened versions), which contain some fixed amount of exact exchange, lead to a large overestimation of the necessary slope correction. Surprisingly, the best solution to this problem is offered by a semilocal potential designed by Becke and Johnson to reproduce the optimized exact exchange potential in free atoms.