wordpress文章添加动态活动倒计时的功能

wordpress文章添加动态活动倒计时的功能

今天要给网站会员涨价的文章里加个倒计时,于是查了下,这个方法实现起来简单点,就用这个好了

1、把下面的代码保存为countdownjs.js,保存在主题的js/目录里:

function getAdd(time){ if(time<10){ return "0"+time; }else{ return time; }}var interval = 1000;function ShowCountDown(year,month,day,hourd,minuted){ var now = new Date(); var endDate = new Date(year, month-1, day, hourd, minuted); var leftTime = endDate.getTime() - now.getTime(); var leftsecond = parseInt(leftTime/1000); var day = Math.floor(leftsecond/(60*60*24)); day = day < 0 ? 0 : day; var hour = Math.floor((leftsecond-day*24*60*60)/3600); hour = hour < 0 ? 0 : hour; var minute = Math.floor((leftsecond-day*24*60*60-hour*3600)/60); minute = minute < 0 ? 0 : minute; var second = Math.floor(leftsecond-day*24*60*60-hour*3600-minute*60); second = second < 0 ? 0 : second; var getDay = getAdd(day); var getHour = getAdd(hour); var getMinute = getAdd(minute); var getSecond = getAdd(second); if(endDate > now){ document.getElementById('time').innerHTML = '活动倒计时:'; document.getElementById('day').innerHTML = getDay +'天'; document.getElementById('hour').innerHTML = getHour +'时'; document.getElementById('min').innerHTML = getMinute +'分'; document.getElementById('sec').innerHTML = getSecond +'秒'; }else{ document.getElementById('countdown').innerHTML= '本次活动已经结束' }}

2、把下面的代码添加到主题的functions.php文件:

function countdown($atts, $content=null) { extract(shortcode_atts(array("time" => ''), $atts)); date_default_timezone_set('PRC'); $endtime=strtotime($time); $nowtime=time(); global $endtimes; $endtimes = str_replace(array("-"," ",":"),",",$time); if($endtime>$nowtime){ return ' <div id="countdown"> <span id="time"></span> <span id="day"></span> <span id="hour"></span> <span id="min"></span> <span id="sec"></span> </div> '; }else{ return '本次活动已经结束'; }}function countdown_js() { global $endtimes; echo '<script>window.setInterval(function(){ShowCountDown('.$endtimes.');}, interval);</script>'."\n";}add_shortcode('countdown', 'countdown');add_action('wp_footer', 'countdown_js');wp_register_script( 'countdown_js', get_template_directory_uri() . '/js/countdownjs.js', array(), '1.0', false );wp_enqueue_script( 'countdown_js' );

3、在文章编辑窗口中添加短代码:

[countdown time="2030-08-15 23:59:59"]

码字不易,转载请注明出处:大海资源网 » wordpress文章添加动态活动倒计时的功能
喜欢(0)

评论抢沙发

评论前必须登录!

免责声明:敬告所有本站用户,大海资源网所有内容皆来源于网络资源,未做安全校验测试,涉及支付、个人信息等重要内容切勿上线运营,视频教学中的源码虽然可以搭建成功运行,但仅作为网络教学使用,不代表没有隐藏后门漏洞等问题,使用前请自行检测,所有后果自行承担。
在线客服

在线客服

  • 扫描二维码,微信联系 QQ扫描二维码