喜欢 bing 搜索的每日背景,以前用在 WordPress 的登录,网上随手抄了一段 放到 function.php 中,可以实现。
但是,有点模糊,发现用的方法是以前的老方法,调用的图片是 1366*768 的大小,改进后为调用 1920x1080 的高清图,与 bing 搜索官网每日背景一致。

原代码:
- function custom_login_head(){
- $str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
- if(preg_match("/<url>(.+?)<\/url>/ies",$str,$matches)){
- $imgurl='http://cn.bing.com'.$matches[1];
- echo'<style type="text/css">body{background: url('.$imgurl.');width:100%;height:100%;background-image:url('.$imgurl.');-moz-background-size: 100% 100%;-o-background-size: 100% 100%;-webkit-background-size: 100% 100%;background-size: 100% 100%;-moz-border-image: url('.$imgurl.') 0;background-repeat:no-repeat\9;background-image:none\9;}</style>';
- }}
- add_action('login_head', 'custom_login_head');
改进后的代码:
- function custom_login_bg(){
- $arr = json_decode(file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'));
- $imgurl = 'http://cn.bing.com'.$arr->images[0]->url;
- echo'<style type="text/css">body{background: url('.$imgurl.') center center no-repeat;-moz-background-size: cover;-o-background-size: cover;-webkit-background-size: cover;background-size: cover;background-attachment: fixed;}</style>';
- }
- add_action('login_head', 'custom_login_bg');
用法:
放置代码到主题的 function.php 文件最后一个 ?> 前;
完毕!
评论 (0)