
wordpressプラグインを使ってカウントダウンタイマーを表示させる方法
お勧めのプラグインは
「Uji Countdown」
です。
wordpressのプラグイン追加画面から
「Uji Countdown」を検索して追加しましょう。
Uji Countdownプラグインを使ったカウントダウンタイマーのサンプルを設置してみました。
東京オリンピック開会式までのカウントダウンタイマー
2020年7月24日 午後8時 開会式
22世紀までのカウントダウンタイマー
西暦2101年1月1日
wordpressにカウントダウンタイマーを設置【備忘録】(Javascript)
次はこの賢威テンプレートで運営しているSEOブログにjavascriptでカウントダウンタイマーを設置してみます。
CSS
<style type="text/css"> #jcd { height: 240px; line-height: 240px; border-top: #ff0000 solid 2px; border-bottom: #ff0000 solid 2px; margin-bottom: 60px; } #jcd span { font-size: 150%; color: #ff0000; } </style>
Javascript
<script type="text/javascript"> function CountdownTimer(elm,tl,mes){ this.initialize.apply(this,arguments); } CountdownTimer.prototype={ initialize:function(elm,tl,mes) { this.elem = document.getElementById(elm); this.tl = tl; this.mes = mes; },countDown:function(){ var timer=''; var today=new Date(); var day=Math.floor((this.tl-today)/(24*60*60*1000)); var hour=Math.floor(((this.tl-today)%(24*60*60*1000))/(60*60*1000)); var min=Math.floor(((this.tl-today)%(24*60*60*1000))/(60*1000))%60; var sec=Math.floor(((this.tl-today)%(24*60*60*1000))/1000)%60%60; var milli=Math.floor(((this.tl-today)%(24*60*60*1000))/10)%100; var me=this; if( ( this.tl - today ) > 0 ){ if (day) timer += '<span class="day">'+day+'日と</span>'; if (hour) timer += '<span class="hour">'+hour+'時間</span>'; timer += '<span class="min">'+this.addZero(min)+'分</span><span class="sec">'+this.addZero(sec)+'秒</span><span class="milli">'+this.addZero(milli)+'</span>'; this.elem.innerHTML = timer; tid = setTimeout( function(){me.countDown();},10 ); }else{ this.elem.innerHTML = this.mes; return; } },addZero:function(num){ return ('0'+num).slice(-2); } } function JCD(){ var text = '2020年元日まで:'; var tl = new Date('2020/01/01 00:00:00'); var timer = new CountdownTimer('JCD',tl,'終了しました。'); timer.countDown(); target = document.getElementById("text"); target.innerHTML = text; } window.onload=function(){ JCD(); } </script>
賢威テンプレートversion7の場合の記入個所
今現在このSEOブログで使っている賢威のversionは7です。
賢威7の場合ダッシュボード内にある
「この投稿だけの個別CSS/JS記述欄」に
上記のCSSとJavascriptを記入する
記述された内容がそのまま出力されるので、CSSであれば<style>タグ、JavaScriptの場合は<script>タグが必要です。
HTMLの記述
<div id="jcd"> <span class="timetext" id="text"></span><span id="JCD"></span> </div>
ダッシュボードの本文欄に上記のコードを記述