月送りカレンダー

月も替わり、7月となりました。
ふとカレンダーを見ると、7月が表示されているのだけど、
前月のカレンダーは見れず・・・
MTの初期状態では、カレンダーは当月のものが必ず表示されます。
その月にエントリーが無くてもね。
月が替わった時にエントリーが無いことって、これからも何度もありそうです。
これは不便だな~と思ったので、いろいろ探してみました。


参考にしたのは、小粋空間さんのエントリー「月送りカレンダー」と
月送りカレンダー・1日に表示されなくなる不具合を改善」です。
「月送りカレンダー」のみでは、当月のエントリー等が未入力の場合にエラーになってしまい、
困っていた時に不具合改善分を見つけたので、一緒に実施しました。
ちなみに、以下のソースではPHP化されていることが前提となります。
PHP化していない人は、以下のソースの .php の部分を .html (または .htm )に変更すると良いと思います。
1.ディレクトリ作成
このカスタマイズでは、カレンダーは別ファイルとして独立することになるので、
まずは専用のディレクトリを作成します。
archive ディレクトリに新たに calendar ディレクトリを作成します。
2.カレンダー用のスタイルシート作成
MTの管理メニューから、「テンプレート」→「インデックステンプレート」右の「新しいインデックステンプレートを作る」をクリック。
次の画面で下記のように設定します。設定が終わったら「保存」をクリックです。
テンプレートの名前:style-calendar
出力ファイル名:calendar.css
テンプレートの中身:

@charset “utf-8″;
body {
background: #ffffff;
margin: 0px 0px 0px 0px;
}
#links {
float: center;
background: #ffffff;
}
#links a {
color: #8FABBE;
text-decoration: none;
}
#links a:hover {
color: #006699;
text-decoration: underline;
}
.calendarhead {
text-align: center;
font-family: Verdana, Arial, sans-serif;
font-size: 9px;
color: #666666;
letter-spacing: .3em;
}
.calendar {
font-family: Verdana, Arial, sans-serif;
color: #666666;
font-weight: normal;
letter-spacing: normal;
text-decoration: none;*/
background: none;
}
.calendar th {
font-size:8px;
width : 20px ;
}
.calendar td {
font-size:9px;
}
.calendar table {
padding: 0px;
border-collapse: collapse;
}

3.カレンダー用アーカイブテンプレートを作成
MTの管理メニューより「テンプレート」→「アーカイブに関連したテンプレート」の
「新しいアーカイブテンプレートを作る」をクリックします。
次の画面で下記のように設定します。設定が終わったら「保存」をクリックです。
テンプレートの名前:カレンダー
テンプレートの内容:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1./ Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=<$MTPublishCharset$>” /><title><$MTBlogName$></title>
<link rel=”stylesheet” href=”<$MTBlogURL$>calendar.css” type=”text/css” />
<MTBlogIfCCLicense>
<$MTCCLicenseRDF$>
</MTBlogIfCCLicense>
</head>
<body >
<div id=”links”>
<div class=”calendar”>
<table summary=”Monthly calendar with links to each day’s posts”>
<caption class=”calendarhead”>
<MTArchivePrevious>
<a href=”<MTBlogURL>archives/calendar/<MTArchiveDate format=”%Y/%m/index”>.php”><</a>
</MTArchivePrevious>
<$MTArchiveDate format=”%B %Y”$>
<MTArchiveNext>
<a href=”<MTBlogURL>archives/calendar/<MTArchiveDate format=”%Y/%m/index”>.php”>></a>
</MTArchiveNext>
</caption>
<tr height=”15″>
<th abbr=”Sunday” align=”center” valign=”bottom”><span class=”calendar”><font color=”#e50003″>Sun</font></span></th>
<th abbr=”Monday” align=”center” valign=”bottom”><span class=”calendar”>Mon</span></th>
<th abbr=”Tuesday” align=”center” valign=”bottom”><span class=”calendar”>Tue</span></th>
<th abbr=”Wednesday” align=”center” valign=”bottom”><span class=”calendar”>Wen</span></th>
<th abbr=”Thursday” align=”center” valign=”bottom”><span class=”calendar”>Thu</span></th>
<th abbr=”Friday” align=”center” valign=”bottom”><span class=”calendar”>Fri</span></th>
<th abbr=”Saturday” align=”center” valign=”bottom”><span class=”calendar”><font color=”blue”>Sat</font></span></th>
</tr>
<MTCalendar month=”this”>
<MTCalendarWeekHeader><tr></MTCalendarWeekHeader>
<td align=”center”><span class=”calendar”>
<!–追加–><MTCalendarIfToday><b></MTCalendarIfToday><!—->
<MTCalendarIfEntries><MTEntries lastn=”1″>
<a href=”<$MTEntryLink archive_type=”Daily”$>” title=”[<$MTEntryTitle$>]” target=”_top”>
<$MTCalendarDay$></a>
</MTEntries></MTCalendarIfEntries>
<MTCalendarIfNoEntries><$MTCalendarDay$></MTCalendarIfNoEntries>
<!–追加–><MTCalendarIfToday></b></MTCalendarIfToday><!—-></span>
<MTCalendarIfBlank> </MTCalendarIfBlank>
</span></td>
<MTCalendarWeekFooter></tr></MTCalendarWeekFooter>
</MTCalendar>
</table>
</div>
</div>
</body>
</html>

4.ファイルの関連付け
MTの管理メニューより「ウェブログの設定」→「アーカイブの設定」の
「新しく、テンプレートとアーカイブを関連付ける」で下記のように設定します。
アーカイブの種類:月別(またはMonthly)
テンプレート:カレンダー
設定後、「保存」をクリック。正常に保存されると「月別」の欄に
カレンダーのテンプレートが追加されます。
追加された部分に下記のように設定します。
ラジオボタン:日付アーカイブ(またはDate-Based Archive)のまま
アーカイブ・ファイルのテンプレート:

<$MTArchiveDate format="calendar/%Y/%m/index."$>.php

5.カレンダーの設置
「テンプレートの編集」 でカレンダーを表示するテンプレートのカレンダー表示用タグを削除し、
下のタグ(青色)と入れ替えてください。
ちなみに、削除するカレンダー表示用タグはデフォルトではこんなのでした。

<div id=”calendar”>
<table summary=”投稿されたエントリーへのリンク付き月間カレンダー”>
<caption><$MTDate format=”%B %Y”$></caption>
<tr>
<th abbr=”日曜日”>日</th>
<th abbr=”月曜日”>月</th>
<th abbr=”火曜日”>火</th>
<th abbr=”水曜日”>水</th>
<th abbr=”木曜日”>木</th>
<th abbr=”金曜日”>金</th>
<th abbr=”土曜日”>土</th>
</tr>
<MTCalendar>
<MTCalendarWeekHeader><tr></MTCalendarWeekHeader>
<td><MTCalendarIfEntries><MTEntries lastn=”1″><a href=”<$MTEntryPermalink$>”><$MTCalendarDay$></a>
</MTEntries></MTCalendarIfEntries>
<MTCalendarIfNoEntries><$MTCalendarDay$></MTCalendarIfNoEntries>
<MTCalendarIfBlank> </MTCalendarIfBlank></td>
<MTCalendarWeekFooter></tr></MTCalendarWeekFooter>
</MTCalendar>
</table>
</div>

この部分を下記のように変更します。
最初は念の為に、上記のカレンダー表示用タグの最初に<!–
最後に –> と記述し、表示されないようにしておくと、
エラーの時に元に戻すのが楽になります。

<?php
$path = ‘path‘;
$url = ‘<$MTBlogURL$>’;
$year = ‘<$MTDate format=”%Y” $>’;
$month = ‘<$MTDate format=”%m” $>’;
$filename = ‘index.php’;
$src = “”;
$file = ‘archives/calendar/’ . $year . ‘/’ . $month . ‘/’ . $filename;
if (file_exists($path . $file)) {
    $src = $url . $file;
} else {
    for ($i = 0; $i < 11; $i++) {
        if ($month != ’01′) {
            $month–;
            $month = substr(’0′. $month, -2);
        } else {
            $year–;
            $month = ’12′;
        }
        $file = ‘archives/calendar/’ . $year . ‘/’ . $month . ‘/’ . $filename;
        if (file_exists($path . $file)) {
            $src = $url . $file;
            $flag = 1;
            break;
        }
    }
}
?>
<div align=”center” class=”side”>
<iframe name=”cal-iframe” src=”<? print $src; ?>” width=”150″ height=”115″ scrolling=”no” frameborder=”0″ marginwidth=”0″ marginheight=”0″></iframe>
</div>

path にはサイトのフルパスを記入します。
例えば、”/home/blog”ってなかんじに。
ロリポップサーバーの場合は、ユーザ情報の確認をするとわかりますよ。
後は、保存して再構築すればOK!です。

One thought on “月送りカレンダー

  1. 月送りカレンダーに変更です。

    カレンダーを変更しました。もう10月ですね〜 参考にしたサイトはこちら 小粋空間…