WordPress website building tutorial: add countdown function to the site

 Watson Blog February 16, 2019 00:15:21 WordPress comment three hundred and twenty-four Reading mode

WordPress website building tutorial: add countdown function to the site

When publishing the activity, the deadline of the activity of the preparer is very necessary. Today's tutorial "WordPress Tutorial: Adding Countdown Function to the Site" is to achieve this function.

1. Save the following code as countdownjs.js in the js/directory of the current theme:

  1. function  getAdd(time){
  2. if (time<10){
  3. return   "0" +time;
  4. } else {
  5. return  time;
  6. }
  7. }
  8. var  interval = 1000;
  9. function  ShowCountDown(year,month,day,hourd,minuted){
  10. var  now =  new  Date();
  11. var  endDate =  new  Date(year, month-1, day, hourd, minuted);
  12. var  leftTime = endDate.getTime() - now.getTime();
  13. var  leftsecond = parseInt(leftTime/1000);
  14. var  day = Math.floor(leftsecond/(60*60*24));
  15. day = day < 0 ?  0 : day;
  16. var  hour = Math.floor((leftsecond-day*24*60*60)/3600);
  17. hour = hour < 0 ?  0 : hour;
  18. var  minute = Math.floor((leftsecond-day*24*60*60-hour*3600)/60);
  19. minute = minute < 0 ?  0 : minute;
  20. var  second = Math.floor(leftsecond-day*24*60*60-hour*3600-minute*60);
  21. second = second < 0 ?  0 : second;
  22. var  getDay = getAdd(day);
  23. var  getHour = getAdd(hour);
  24. var  getMinute = getAdd(minute);
  25. var  getSecond = getAdd(second);
  26. if (endDate > now){
  27. Document. getElementById ('time '). innerHTML=' Activity countdown: ';
  28. Document. getElementById ('day '). innerHTML=getDay+' day ';
  29. Document. getElementById ('hour '). innerHTML=getHour+' when ';
  30. Document. getElementById ('min '). innerHTML=getMinute+' minute ';
  31. Document. getElementById ('sec '). innerHTML=getSecond+' seconds';
  32. } else {
  33. Document. getElementById ('countdown '). innerHTML='This activity has ended'
  34. }
  35. }

2. Add the following code to the last of the functions.php file of the current theme?> In front of:

  1. function  countdown( $atts $content =null) {
  2. extract(shortcode_atts( array ( "time"  =>  '' ),  $atts ));
  3. date_default_timezone_set('PRC');
  4. $endtime = strtotime ( $time );
  5. $nowtime =time();
  6. global   $endtimes ;
  7. $endtimes  =  str_replace ( array ( "-" , " " , ":" ), "," , $time );
  8. if ( $endtime > $nowtime ){
  9. return  '
  10. <div id= "countdown" >
  11. <span id= "time" ></span>
  12. <span id= "day" ></span>
  13. <span id= "hour" ></span>
  14. <span id= "min" ></span>
  15. <span id= "sec" ></span>
  16. </div>
  17. ';
  18. } else {
  19. return 'This activity has ended';
  20. }
  21. }
  22. function  countdown_js() {
  23. global   $endtimes ;
  24. echo  '<script>window.setInterval( function (){ShowCountDown('. $endtimes .');},  interval);</ script>'. "\n" ;
  25. }
  26. add_shortcode('countdown', 'countdown');
  27. add_action('wp_footer', 'countdown_js');
  28. wp_register_script( 'countdown_js', get_template_directory_uri() .  '/js/countdownjs.js',  array (), '1.0', false );
  29. wp_enqueue_script( 'countdown_js' );

3. When publishing/updating an article, switch to the end of the article mode, and then add the following short code where you want to insert the countdown:

  1. [countdown time= "2019-01-15 18:41:57" ]

 

The time in the quotation marks of time="2019-01-15 18:41:57" is the end time of the activity. Please keep the format consistent when changing to other dates and times.

 

Reprinted from: https://jiub.ren/3060.html

 Watson Blog
  • This article is written by Published on February 16, 2019 00:15:21
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/1624.html

Comment