简体   繁体   中英

Adapt length of Wordpress excerpt to screen width

I'm making an WP theme, but I have a little problem. I want the length of excerpt (in carachters) to be adaptive to the screen width.

  • if screen width is bigger then 1000px, 20 characters
  • between 1000 and 700 it should be 5
  • between 700 and 640px, 20 chars
  • between 640 and 480, 5 chars
  • below 480px, 20 chars.

Hope that this makes sense. :)

I tried creating something in my functions.php. I used methods as:

function new_excerpt_length($length) {

    if ((screen.width > 1024))
    {
        return 5;
    }
    else
    {
        return 15;
    }
}
add_filter('excerpt_length', 'new_excerpt_length');

But that didn't work, it outputs the same number. Css isn't a solution, has to be done in JS, in the functions.php file (no jQuery)

Sorry to burst your bubble here, but there is no way for php to know which screen size the information is served to. Detecting of screen size is only done on client/browser side.

The nearest you are going to get here related to Wordpress is with wp_is_mobile() which can detect mobile devices, but then again, wp_is_moble() is more that a joke as a proper function.

Your only solution here would be to look at jquery to truncate the output text according to browser size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM