转换Wordpress页面上的所有Unix时间戳字符串

时间:2016-03-28 10:46:57

标签: javascript php jquery wordpress unix-timestamp

我使用名为EventOn的Wordpress插件在网站上创建活动。该插件为每个事件创建一个自定义帖子,并将事件日期存储在名为 evcal_srow 的元键中 其中值存储在Unix时间内(例如1464739500)。

现在我想在这里实现的是通过Slider Revolution提供的这些自定义帖子,使用滑块提供的短代码从自定义帖子中提取自定义字段,在这种特殊情况下,短代码是 {{元:evcal_srow}}

以下是我通过滑块revo的导航编辑器生成缩略图的方法:

<span class="tp-thumb-image">

        <h3 class="thumb-event-title">
          {{title}}
        </h3>

        <h2 class="thumb-event-date" style="color:#fff">
          {{meta:evcal_srow}}
        </h2>

</span>

一切正常,因为我能够成功提取帖子标题,精选图​​片和日期,但我需要将日期转换为人格式。

如果我正在创建普通页面,那么我将创建一个模板文件并添加类似

的内容
<?php
    $ts = get_post_meta($post->ID, 'evcal_srow', true);
    $date = new DateTime("@$ts");
    echo "<div class='datum'>".$date->format('d.m.Y')."</div><div class='uhrzeit'>".$date->format('H:i')."</div>";
?>

虽然我通过滑块设置拉取数据的方式,所以在这种情况下我无法使用php,因为它不被允许(除非你当然有更好的想法!)。此外,我无法直接编辑插件文件,因为我需要在将来更新它,并且我不确定插件的子主题是否有任何类似内容。

我想到的第一件事是在页面加载后转换页面中的所有选择器 .thumb-event-date ,也许使用一些JS或jQuery,I&#39我试过很多方法,但不能得到任何积极的结果。

如果任何人能够提出解决方案,那将是非常好的。 非常感谢你的时间。

解决方案(非常感谢Paul!)

  • 转到滑块设置
  • 找到自定义Javascript 部分和 将其粘贴到其中:

    revapi1.on('revolution.slide.onloaded', function() { // Replace the number 1 in revapi1.on with the ID of your slider
    
        jQuery('.thumb-event-date').each(function(index, value) { // Replace .thumb-event-date with the selector that contains the unix time string
            jQuery(value).html(new Date(jQuery(value).html()*1000).toISOString()); 
        }); 
    

    });

1 个答案:

答案 0 :(得分:0)

$('.thumb-event-date').each(function(index, value) {
  $(value).html(new Date($(value).html()*1000).toISOString()); // put whatever Date() method you want/need (to show human readable format) 
});