$args = array( 'numberposts' => '10', 'post_type' => 'newposttype');
$recent_posts = wp_get_recent_posts( $args );

I know it's not a simple thing to ask but how could I set the numberposts depending on the screen width?

That is: for a desktop visitor I show 10 posts and for a mobile visitor I show 3 posts.

  • 1
    There is no clean solution to this and no way in php to determine screen width – Pieter Goosen Jan 29 '15 at 4:18

Yes, there is no way to get screen size with PHP, because it runs on server, while the screen is something related to client (browser).

However, in OP you say:

for a desktop visitor I show 10 posts and for a mobile visitor I show 3 posts

and even if you can't get the screen size, you can understand if the request comes from a mobile device, thanks to wp_is_mobile():

$number = 10;
if (wp_is_mobile()) {
  $number = 3;
$args = array( 'numberposts' => $number, 'post_type' => 'newposttype');
$recent_posts = wp_get_recent_posts( $args );

There are PHP libraries like Mobile Detect that gives you more control, e.g. you can differentiate tablets form phones.

So if post number choice may depend on device being mobile / non-mobile, than it can be easily done as explained above, if the choice must depend on real screen width than the only solution is AJAX.

Search this site (start here) to find guidance on how to get posts using AJAX.

  • Server-side device detection is no very reliable, and wp_is_mobile() is not very reliable neiter. – cybmeta Jan 29 '15 at 7:50
  • wp_is_mobile() is useful as a joke as @toscho always say :-) – Pieter Goosen Jan 29 '15 at 7:59
  • wp_is_mobile() is not 100% reliable, sure. But for this kind of things can be used with no issue. 90% of the times it will work, the other 10% people will see 10 posts instead of 3 in their smartphones. Nobody will die. @PieterGoosen – gmazzap Jan 29 '15 at 8:34
  • @cybmeta see above – gmazzap Jan 29 '15 at 8:35
  • I do agree on that. At the end of the day, it all depends if 90% is good enough enough for you taking into consideration with what you are asking or looking to do, and most importantly, you know that the route you are about to take is not very reliable and it will fail in some instances – Pieter Goosen Jan 29 '15 at 8:47

There is no way to determine the screen size with PHP (which you would need to do in order to alter that variable).

So you have two options:

1) Easy: Use responsive CSS to hide posts 4-10 on smaller screens

In your loop, add a condition class to any post after the third post, like this:

$count = 0;
$args = array( 'numberposts' => '10', 'post_type' => 'newposttype');
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    $count ++; ?>
    <div class="<?php if($count < 3) echo 'hidden-xs'; ?>"><a href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"]</a></div>
} ?>

Then add a css class within a media query to only hide that class if the screen is smaller than X pixels.

@media (max-width: 600px) {

2) Harder: Load the posts via AJAX so you can use Javascript to determine the screen width and then request the matching number of posts.

I won't go into the details on this one, as the first option should suffice.

  • I would not use CSS to simply hide the posts. Use php and simply skip them, something like if( $count > 3 ) continue;. Hidden elements still appear in the browser console – Pieter Goosen Jan 29 '15 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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