简体   繁体   English

使 Wordpress 摘录的长度适应屏幕宽度

[英]Adapt length of Wordpress excerpt to screen width

I'm making an WP theme, but I have a little problem.我正在制作一个 WP 主题,但我有一个小问题。 I want the length of excerpt (in carachters) to be adaptive to the screen width.我希望摘录的长度(以 carachters 为单位)适应屏幕宽度。

  • if screen width is bigger then 1000px, 20 characters如果屏幕宽度大于 1000 像素,20 个字符
  • between 1000 and 700 it should be 5在 1000 到 700 之间应该是 5
  • between 700 and 640px, 20 chars 700 到 640 像素之间,20 个字符
  • between 640 and 480, 5 chars 640 到 480 之间,5 个字符
  • below 480px, 20 chars.低于 480 像素,20 个字符。

Hope that this makes sense.希望这是有道理的。 :) :)

I tried creating something in my functions.php.我尝试在我的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) Css 不是解决方案,必须在 JS 中完成,在 functions.php 文件中(没有 jQuery)

Sorry to burst your bubble here, but there is no way for php to know which screen size the information is served to.很抱歉在这里打破您的泡沫,但是 php 无法知道信息提供给哪个屏幕尺寸。 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.与 Wordpress 相关的最接近的地方是wp_is_mobile() ,它可以检测移动设备,但话又说回来, wp_is_moble()更像是一个适当的功能的笑话。

Your only solution here would be to look at jquery to truncate the output text according to browser size.您在这里唯一的解决方案是查看 jquery 根据浏览器大小截断输出文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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