如何使jQuery倒计时定时器功能手动编辑

时间:2020-06-10 作者:user9637601

我有一个wordpress主题,我想在首页显示某个促销开始时间。它使用jquery。倒计时。min.js并具有以下默认计时器设置

var siteCountDown = function() {

        if ( $(\'#date-countdown\').length > 0 ) {
            $(\'#date-countdown\').countdown(\'2020/10/10\', function(event) {
              var $this = $(this).html(event.strftime(\'\'
                + \'<span class="countdown-block"><span class="label">%w</span> weeks </span>\'
                + \'<span class="countdown-block"><span class="label">%d</span> days </span>\'
                + \'<span class="countdown-block"><span class="label">%H</span> hr </span>\'
                + \'<span class="countdown-block"><span class="label">%M</span> min </span>\'
                + \'<span class="countdown-block"><span class="label">%S</span> sec</span>\'));
            });
        }

    };
    siteCountDown();
它显示,促销活动将在四个月后开始。但我想通过wordpress管理仪表板手动设置计时器。是否可以在中插入一些php代码.countdown(\'2020/10/10\' 类似于

<?php if ($year = get_option(\'of_year\') && $month = get_option(\'of_month\') &&  $day = get_option(\'of_day\') ) { ?>
.countdown(\'<?php echo $year; ?>/<?php echo $month; ?>/<?php echo $day; ?>
<?php }  ?>
这样我就可以手动编辑促销开始时间

我的选择是:

$options[] = array( "name" => "Year",
                    "desc" => "promo start year",
                    "id" => $shortname."_promo_year",
                    "std" => "",
                    "type" => "text"); 

$options[] = array( "name" => "Mount",
                    "desc" => "promo start month",
                    "id" => $shortname."_promo_month",
                    "std" => "",
                    "type" => "text");

$options[] = array( "name" => "Day",
                    "desc" => "promo start day",
                    "id" => $shortname."_promo_day",
                    "std" => "",
                    "type" => "text");

1 个回复
SO网友:Sabbir Hasan

您可以使用wp_footer 行动将以下代码添加到functions.php 文件它将在页面的页脚加载脚本。

add_action(\'wp_footer\', \'load_count_down\');

function load_count_down(){
    if ($year = get_option(\'of_year\') && $month = get_option(\'of_month\') &&  $day = get_option(\'of_day\') ) {

        $date = $year.\'/\'.$month.\'/\'.$day;
        $script = <<<EOF
<script type="text/javascript">

var siteCountDown = function() {
    if ( $(\'#date-countdown\').length > 0 ) {
        $(\'#date-countdown\').countdown(\'$date\', function(event) {
          var \\$this = $(this).html(event.strftime(\'\'
            + \'<span class="countdown-block"><span class="label">%w</span> weeks </span>\'
            + \'<span class="countdown-block"><span class="label">%d</span> days </span>\'
            + \'<span class="countdown-block"><span class="label">%H</span> hr </span>\'
            + \'<span class="countdown-block"><span class="label">%M</span> min </span>\'
            + \'<span class="countdown-block"><span class="label">%S</span> sec</span>\'));
        });
    }
};
siteCountDown();
</script>
EOF;

    echo $script;
    }
}

相关推荐

调用具有多个参数的PHP函数,该函数位于AJAX处理程序之外

Q: 使用AJAX处理程序中的多个参数调用PHP函数的正确方法是什么?Q: 我调用php函数是否正确?Q: 有没有更简单的方法? Page Element 用户单击按钮。Action: 单击将调用php代码,使用以下参数连接到外部服务器:用户名2。IP地址3。端口4。公钥5。服务Purpose:将投票字符串(从主机A)发送到外部游戏服务器(主机B)我不需要更新任何WordPress内容。注意:我拥有这两台服务器。WordPress Version: 5.4.1 PHP Version on