很多WordPress站点前端的背景色都是白色,所以有些站长就想着添加一个背景图片,最好能够添加必应的每日一图为背景,这样每天都能看到不一样的背景图,那么应该如何实现呢?下面boke112百科就跟大家说一说这个WordPress添加背景图的问题。
情况一、WordPress前端添加固定背景图片
这种情况,boke112百科前面跟大家分享的『WordPress站点添加背景图的两种方法』一文中就介绍了两种方法,这里就再次介绍一遍其中修改style.css文件的方法,只需要将以下代码添加到当前主题的 style.css文件即可(记得修改代码中的图片地址,最好是大图哦)。
body {
background-image: url(http://localhost/wordpress/wp-content/uploads/2017/11/timg.jpg);
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
情况二、WordPress前端添加必应每日一图为背景
想要让WordPress前端每天都调用必应每日一图作为背景图,则只需要将以下代码添加到当前主题的functions.php文件中并保存更新文件即可。
//调用 bing 美图作为登录页背景图
function custom_login_head(){
$str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
if (preg_match("/\/(.+?).jpg/", $str, $matches)) {$imgurl='http://s.cn.bing.net'.$matches[0];}
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;background-attachment:fixed;}</style>';
}
add_action('wp_footer', 'custom_login_head');
如果还想让WordPress后台登录页也调用必应每日一图作为背景图的话,则只需要在上述代码后面再添加以下代码并保存即可。
add_action('login_head', 'custom_login_head');

















桂公网安备 45010502000016号