Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript在wordpress中自定义字段计算_Javascript_Wordpress_Countdown_Custom Fields_Calculation - Fatal编程技术网

使用Javascript在wordpress中自定义字段计算

使用Javascript在wordpress中自定义字段计算,javascript,wordpress,countdown,custom-fields,calculation,Javascript,Wordpress,Countdown,Custom Fields,Calculation,我已经在我的站点中实现了下面的代码,以显示正在运行的计时器。该网站正在Wordpress上运行。目前,代码中输入了日期(因此它适用于整个站点)。我希望有一个运行在每个岗位计时器 我需要更改下面的代码,以便在每篇文章中使用名为“到期日”的自定义字段,而不是下面的硬连线日期(newDate(“2021年1月5日15:37:25”)。getTime()作为日期 //确定我们倒计时的日期 var countDownDate=新日期(“2021年1月5日15:37:25”).getTime(); //

我已经在我的站点中实现了下面的代码,以显示正在运行的计时器。该网站正在Wordpress上运行。目前,代码中输入了日期(因此它适用于整个站点)。我希望有一个运行在每个岗位计时器

我需要更改下面的代码,以便在每篇文章中使用名为“到期日”的自定义字段,而不是下面的硬连线日期(newDate(“2021年1月5日15:37:25”)。getTime()作为日期


//确定我们倒计时的日期 var countDownDate=新日期(“2021年1月5日15:37:25”).getTime(); //每1秒更新一次倒计时 var x=setInterval(函数(){ //获取今天的日期和时间 var now=new Date().getTime(); //找出现在和倒计时日期之间的距离 var距离=倒计时日期-现在; //天、小时、分钟和秒的时间计算 变量天数=数学楼层(距离/(1000*60*60*24)); 可变小时数=数学楼层((距离%(1000*60*60*24))/(1000*60*60)); var分钟=数学楼层((距离%(1000*60*60))/(1000*60)); var秒=数学楼层((距离%(1000*60))/1000); //使用id=“demo”在元素中显示结果 document.getElementById(“demo”).innerHTML=days+d+hours+h +分钟+米+秒+秒; //如果倒计时结束,写一些文字 如果(距离<0){ 净间隔(x); document.getElementById(“demo”).innerHTML=“过期”; } }, 1000);
以上代码来源于

我的网站是


提前感谢

以下步骤是您的要求:

1) 在自定义字段中,将返回格式设置为自定义“F j,Y g:i:s”

示例链接()

2) 在functions.php中添加此函数

function functionname() {

    global $post;

    $field= get_field('expiry_date', $post->ID);
        echo '<input type="hidden" id="date" value="'.$field.'">';  
     }
    add_action( 'template_redirect', 'functionname' );
函数functionname(){
全球$员额;
$field=get\u字段('expiration\u date',$post->ID);
回声';
}
添加操作('template_redirect','functionname');
3) 在js文件中添加以下脚本

 var $= jQuery;

    var d = $("#date").val();
    console.log(d);
    // Set the date we're counting down to
    var countDownDate = new Date(d).getTime();

    // Update the count down every 1 second
    var x = setInterval(function() {

      // Get today's date and time
      var now = new Date().getTime();

      // Find the distance between now and the count down date
      var distance = countDownDate - now;

      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);

      // Display the result in the element with id="demo"
      document.getElementById("demo").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";

      // If the count down is finished, write some text
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
      }
    }, 1000);
var$=jQuery;
var d=$(“#日期”).val();
控制台日志(d);
//确定我们倒计时的日期
var countDownDate=新日期(d).getTime();
//每1秒更新一次倒计时
var x=setInterval(函数(){
//获取今天的日期和时间
var now=new Date().getTime();
//找出现在和倒计时日期之间的距离
var距离=倒计时日期-现在;
//天、小时、分钟和秒的时间计算
变量天数=数学楼层(距离/(1000*60*60*24));
可变小时数=数学楼层((距离%(1000*60*60*24))/(1000*60*60));
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
//使用id=“demo”在元素中显示结果
document.getElementById(“demo”).innerHTML=days+d+hours+h
+分钟+米+秒+秒;
//如果倒计时结束,写一些文字
如果(距离<0){
净间隔(x);
document.getElementById(“demo”).innerHTML=“过期”;
}
}, 1000);
确保您必须在要在post中显示的位置添加


我已经尝试过这个代码。它完全工作正常。我希望我在这方面帮助过你。

嗨,奎师那,谢谢你的回复。我去过海外,没有机会看这个,所以感谢你的耐心。我已经更新了所有代码,虽然它没有返回任何错误,但它不会在帖子页面上打印日期。你能请求吗e看一看,看我可能做得不正确吗?请注意,我已将Id(演示)的所有实例更改为(date\u exp)。它仍然不工作。我不确定我做错了什么。我是否需要向“date\u exp”的自定义字段添加Id包装"? 提前谢谢。@JasonStancombe很抱歉回复晚了,我已经编辑了我的代码,是的,你需要在post自定义字段中添加未来日期,请参见ss(),我只是简单地使用我上面的代码,它的工作只是按照我的步骤进行。我附上了管理员的图片,所以这就是我为此所做的一切。。
 var $= jQuery;

    var d = $("#date").val();
    console.log(d);
    // Set the date we're counting down to
    var countDownDate = new Date(d).getTime();

    // Update the count down every 1 second
    var x = setInterval(function() {

      // Get today's date and time
      var now = new Date().getTime();

      // Find the distance between now and the count down date
      var distance = countDownDate - now;

      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);

      // Display the result in the element with id="demo"
      document.getElementById("demo").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";

      // If the count down is finished, write some text
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
      }
    }, 1000);