很多wordpress博客有几个背景,不过需要刷新才能切换,运用的方法是用php随机输出,如果你们需要,也可以拿去用。
将下列代码添加到functions.php里面
function BYMT_rand_bg () {
$randbg = mt_rand(1, 8); /*背景图片随机数*/
$randbgurl = get_bloginfo(‘template_directory’).’/images/background/background’.$randbg.’.jpg’; /*背景图片地址*/
echo'<style>body{background: url(‘.$randbgurl.’) no-repeat top center;background-attachment:fixed;}</style>’.”n”;
}
add_action(‘wp_head’, ‘BYMT_rand_bg’, 100);
下面我介绍一个无刷新自动切换背景的方法,运用的是jQuery的setInterval。
将下列代码添加到主题jQ文件里,或者直接用<script type=”text/javascript”></script>包上放在页脚代码里。
//背景自动切换
jQuery(document).ready(function(){
setInterval(function() {
var imgpath = ‘../wp-content/themes/BYMT/images/background/’; /*背景图片目录*/
var imgrand = Math.floor(Math.random() * 8 + 1); /*背景图片随机数*/
var randurl = ‘<style>body{background: url(‘ + imgpath +’background’+ imgrand + ‘.jpg) no-repeat top center;background-attachment:fixed;}</style>’;
$(“#randbg”).html(randurl);
},10000); /*自动切换时间(毫秒)*/
});
接着在主题<body>后面添加上:
<div id=”randbg”></div>
OK,代码就是这样。
归档
- 2023年5月
- 2023年3月
- 2023年2月
- 2023年1月
- 2022年10月
- 2022年9月
- 2022年8月
- 2022年7月
- 2022年6月
- 2022年5月
- 2022年4月
- 2022年3月
- 2022年2月
- 2022年1月
- 2021年12月
- 2021年11月
- 2021年10月
- 2021年9月
- 2021年8月
- 2021年7月
- 2021年6月
- 2021年5月
- 2021年4月
- 2021年3月
- 2021年2月
- 2021年1月
- 2020年12月
- 2020年11月
- 2020年10月
- 2020年9月
- 2020年8月
- 2020年7月
- 2020年6月
- 2020年5月
- 2020年4月
- 2020年3月
- 2020年2月
- 2019年12月
- 2019年11月
- 2019年9月
- 2019年8月
- 2019年7月
- 2019年3月
- 2019年2月
- 2019年1月
- 2018年12月
- 2018年11月
- 2018年10月
- 2018年9月
- 2018年8月
- 2018年7月
- 2018年5月
- 2018年4月
- 2018年3月
- 2018年2月
- 2018年1月
- 2017年10月
- 2017年9月
- 2017年8月
- 2017年7月
- 2017年6月
- 2017年5月
- 2017年3月
- 2017年2月
- 2017年1月
- 2016年12月
- 2016年11月
- 2016年10月
- 2016年9月
- 2016年8月
- 2016年7月
- 2016年6月
- 2016年5月
- 2016年4月
- 2016年3月
- 2016年2月
- 2016年1月
- 2015年11月
- 2015年10月
- 2015年9月
- 2015年7月
- 2015年5月
- 2015年4月
- 2015年3月
- 2015年2月
- 2015年1月
- 2014年12月
- 2014年11月
- 2014年10月
- 2014年9月
- 2014年8月
- 2014年7月
- 2014年6月
- 2013年6月
- 2012年11月
- 2011年6月
- 2010年2月
- A gray cat slinks past a wooden house. There’s something a little intimidating attempting to describe.
- Error: Please check if you enter Instagram username and Access Token in Theme Setting > Social Profiles
Leave a reply