WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this code on my header:


But I want to show 4 posts on desktop and 2-3 on mobile. In CSS I could use @mediaqueries, could I use something like that on PHP?


share|improve this question

WordPress has a function called wp_is_mobile. It does very simple User Agent checking, which isn't very reliable. There are a lot of user agents out there and they can easily be falsified. There is no guarantee that a browser representing itself as some user agent actually is that user agent. You can do more complicated user agent checking but it is still user agent checking.

But you also don't have a lot of choice with a server side script. I would use wp_is_mobile, or some more complicated version, to hedge my bets. It may save some load time in some, maybe many, cases. But make sure that media queries deal with the important stuff.

share|improve this answer
While this is technical options I want to stress that this is not reliable server side. Won't work at all (or will work faulty) with static page caching, etc. – Rarst May 1 '13 at 17:34
I tried to make an smaller div with overflow hidden, but didn't work because some elements still show after that. – euDennis May 2 '13 at 11:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.