3

I'm trying to limit my posts on Wordpress if the screen width is less than 480px (mobile device, responsive).

However I ran into problem as I found out you cannot use PHP to detect screen width, which I require because I'm using PHP to adjust the post numbers. I was hoping for something like:

<?php /* Start the Loop */ ?>
<?php if media-screen < 480px {
    query_posts('posts_per_page=5'); } ?>
<?php while (have_posts()) : the_post(); ?>

Any suggestions? Can you somehow pass a css/javascript boolean into the php script?

EDIT: I'd rather not redirect visitors to a mobile site as that's way out of my league.

klipach
  • 818
  • 7
  • 21
Ming
  • 744
  • 4
  • 13
  • 26
  • You can do it with AJAX. Not with PHP or CSS. – putvande Dec 31 '13 at 10:27
  • To do width detection in PHP is not a good idea. The better way is to use Media queries https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries or JS http://stackoverflow.com/questions/5484578/how-to-get-document-height-and-width-without-using-jquery – klipach Dec 31 '13 at 10:30
  • @putvande of course it can be done with CSS. Depending on the structure of the DOM some elements could simply be hidden by setting `display: none;` based on some responsive condition. – nietonfir Dec 31 '13 at 10:34
  • The server doesn't care (nor should it care) about the viewport of the end user/client. There are other, more suitable technologies and programming languages that do a far better job at making your UI responsive (JavaScript, CSS, HTML5, ...) use those. – Elias Van Ootegem Dec 31 '13 at 10:36
  • The problem is I can't use javascript/css to toggle the posts. – Ming Dec 31 '13 at 10:48
  • Then what you want to achieve is not possible, sry. – nietonfir Dec 31 '13 at 10:57

3 Answers3

5

I'm not a fan of this solution, but you could simply add an element with the number of posts you want to show on mobile that is hidden by default and only shown when the media-query condition is met.

Imagine the following html containing your mobile posts

<div class="is-mobile">
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
</div>
<div class="is-default>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
    <div class="im-a-post">some content</div>
</div>

The via a simple media query you toggle the visibiliy of your wrapper elements:

.is-mobile {
    display: none;
}
@media (max-width: 480px) {
    .is-default {
        display: none;
    }
    .is-mobile {
        display: block;
    }
}

A more sophisticated (and imho better) approach would be to either annotate the elements you would want to hide (by adding a class) via javascript/php or even CSS3 :nth-child() selectors. Imagine the following javascript loop

// assuming jQuery
$(".posts").each(function(idx, ele) {
    if (idx >= 5) {
        $(ele).addClass("hidden-mobile");
    }
});

with this CSS

@media (max-width: 480px) {
    .hidden-mobile {
        display: none;
    }
}

Together those would hide all but the first five posts on a device where the viewport matches. But then you would have to take pagination properly into account.

nietonfir
  • 4,797
  • 6
  • 31
  • 43
  • Honestly thought there would be a better approach. I assume that's why you simply redirect all mobile users. This has to do for now, thanks- – Ming Dec 31 '13 at 11:04
  • You don't redirect nowadays anymore, RWD ftw! ;-) It's basically the apporach outlined in the solution, just more sophisticated. One could e.g. create a dummy pagination that toggles the visibility of the mobile hidden elements in blocks. Or, far better nowadays at least if you think in "mobile-first" terms, you would only have the mobile posts in the DOM and then load others asynchronously if it's not a mobile device. – nietonfir Dec 31 '13 at 11:12
3

I think you may benefit from using 51Degrees PHP solution. It is essentially a device detector that uses User Agent string in order to match device against devices from database file. It is available as a Wordpress plugin and can be configured through the admin panel.

You can use this plugin to set up a rule that supplies a different theme for devices with the desired screen size. You can set up as many rules as you want with many different combinations. With the basic device info you have over 50 device properties to choose from:

51Degrees Wordpress admin page showing rule creation with additional properties

This way you can supply a different theme that will show less posts for mobile devices without having to do any coding (you can supply an entirely different theme or copy your current theme but change number of posts to 5). In case you don't want to add plugins to your Wordpress you can add same functionality by downloading the detector from sourceforge and adding the following lines of code to your theme:

<?php
//Add device detector to project.
require_once 'path/to/core/51Degrees.php';
require_once 'path/to/core/51Degrees_usage.php';

//Use information about device.
if ($_51d['ScreenPixelsWidth'] == 400)
{
     query_posts('posts_per_page=5');
}
?>

Hope this helps.

Mike
  • 144
  • 8
-1

Use this function for mobile condition

function is_mobile() {
    static $is_mobile;
    if ( isset($is_mobile) )
        return $is_mobile;
    if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
        $is_mobile = false;
    } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
            $is_mobile = true;
    } else {
        $is_mobile = false;
    }
    return $is_mobile;
}
Tobias K.
  • 2,997
  • 2
  • 12
  • 29
maulik1987
  • 1
  • 1
  • 8
  • There are similar solutions (regex based / package) that may be more complete: https://stackoverflow.com/q/4117555/7362396 https://github.com/serbanghita/Mobile-Detect – Tobias K. Jun 23 '21 at 14:53