最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to make jquery count down timer function manually editable

programmeradmin0浏览0评论

I have a wordpress theme and I would like to display a certain promo start time in front page. It uses jquery.countdown.min.js and has following default timer settings

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();

It displays that promo will start in four months later after now. But I would like to set timer manually via wordpress admin dashboard. Is it possible to insert some php codes in .countdown('2020/10/10' something like

<?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 }  ?>

so that I can edit promo start time manually

My options are:

$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");

I have a wordpress theme and I would like to display a certain promo start time in front page. It uses jquery.countdown.min.js and has following default timer settings

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();

It displays that promo will start in four months later after now. But I would like to set timer manually via wordpress admin dashboard. Is it possible to insert some php codes in .countdown('2020/10/10' something like

<?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 }  ?>

so that I can edit promo start time manually

My options are:

$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");
Share Improve this question edited Jun 10, 2020 at 21:32 user9637601 asked Jun 10, 2020 at 18:40 user9637601user9637601 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can achieve this using wp_footer action. Add bellow code to functions.php file. It will load the script at the footer of page.

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论