Retoucher.jp

主にPC関連の備忘録と映画の感想を書いています。

ホーム > PC関連備忘録 > WordPress > カレンダーを無理矢理カスタマイズ

カレンダーを無理矢理カスタマイズ

プラグインを作れる能力があれば作っているのですが、その技術力が無いのでデフォルトテンプレートをイジってカレンダーをカスタマイズしてみました。

このやり方だとアップグレードの際に書き換えられてしまうので、問題ありなんですがテストも兼ねているので、仕方ないです。

念のため、オリジナルはコピーして保存しておきました。
/wordpressディレクトリ/wp-includes/general-template.php


単純にテーブルのカレンダーではなく、1行のカレンダーがほしかったので、その部分を分解して再構築しただけです。

約510行目
<table>タグがある部分を

echo ‘<div id=”custom-calendar”>’;

/*前の月*/
if ( $previous ) {
echo “\n\t\t”.’<span abbr=”‘ . $wp_locale->get_month($previous->month) . ‘” id=”prev”><a href=”‘ .
get_month_link($previous->year, $previous->month) . ‘” title=”‘ . sprintf(__(‘View posts for %1$s %2$s’), $wp_locale->get_month($previous->month),
date(‘Y’, mktime(0, 0 , 0, $previous->month, 1, $previous->year))) . ‘”>« Prev</a></span>’;
} else {
echo “\n\t\t”.’<span id=”prev” class=”pad”> </span>’;
}

// Get days with posts
$dayswithposts = $wpdb->get_results(“SELECT DISTINCT DAYOFMONTH(post_date)
FROM $wpdb->posts WHERE MONTH(post_date) = ‘$thismonth’
AND YEAR(post_date) = ‘$thisyear’
AND post_type = ‘post’ AND post_status = ‘publish’
AND post_date < ‘” . current_time(‘mysql’) . ‘\”, ARRAY_N);
if ( $dayswithposts ) {
foreach ( $dayswithposts as $daywith ) {
$daywithpost[] = $daywith[0];
}
} else {
$daywithpost = array();
}

if (strpos($_SERVER['HTTP_USER_AGENT'], ‘MSIE’) !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), ‘camino’) !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), ‘safari’) !== false)
$ak_title_separator = “\n”;
else
$ak_title_separator = ‘, ‘;

$ak_titles_for_day = array();
$ak_post_titles = $wpdb->get_results(“SELECT post_title, DAYOFMONTH(post_date) as dom ”
.”FROM $wpdb->posts ”
.”WHERE YEAR(post_date) = ‘$thisyear’ ”
.”AND MONTH(post_date) = ‘$thismonth’ ”
.”AND post_date < ‘”.current_time(‘mysql’).”‘ ”
.”AND post_type = ‘post’ AND post_status = ‘publish’”
);
if ( $ak_post_titles ) {
foreach ( $ak_post_titles as $ak_post_title ) {

$post_title = apply_filters( “the_title”, $ak_post_title->post_title );
$post_title = str_replace(‘”‘, ‘"’, wptexturize( $post_title ));

if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
$ak_titles_for_day['day_'.$ak_post_title->dom] = ”;
if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
$ak_titles_for_day["$ak_post_title->dom"] = $post_title;
else
$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
}
}

// See how much we should pad in the beginning
$pad = calendar_week_mod(date(‘w’, $unixmonth)-$week_begins);
if ( 0 != $pad )
echo “\n\t\t”.’<span class=”pad”> </span>’;

$daysinmonth = intval(date(‘t’, $unixmonth));
for ( $day = 1; $day <= $daysinmonth; ++$day ) {
if ( isset($newrow) && $newrow )
echo “”;
$newrow = false;

if ( $day == gmdate(‘j’, (time() + (get_option(‘gmt_offset’) * 3600))) && $thismonth == gmdate(‘m’, time()+(get_option(‘gmt_offset’) * 3600)) && $thisyear == gmdate(‘Y’, time()+(get_option(‘gmt_offset’) * 3600)) )
echo ‘<span id=”today”>’;
else
echo ‘<span>’;

if ( in_array($day, $daywithpost) ) // any posts today?
echo ‘<a href=”‘ . get_day_link($thisyear, $thismonth, $day) . “\” title=\”$ak_titles_for_day[$day]\”>$day</a>”;
else
echo $day;
echo ‘</span>’;

if ( 6 == calendar_week_mod(date(‘w’, mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
$newrow = true;
}

$pad = 7 – calendar_week_mod(date(‘w’, mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
if ( $pad != 0 && $pad != 7 )
echo “\n\t\t”.’<span> </span>’;

/*次の月*/
if ( $next ) {
echo “\n\t\t”.’<span abbr=”‘ . $wp_locale->get_month($next->month) . ‘” id=”next”><a href=”‘ .
get_month_link($next->year, $next->month) . ‘” title=”‘ . sprintf(__(‘View posts for %1$s %2$s’), $wp_locale->get_month($next->month),
date(‘Y’, mktime(0, 0 , 0, $next->month, 1, $next->year))) . ‘”>’ .’Next »</a></span>’;
} else {
echo “\n\t\t”.’<span id=”next” class=”pad”> </span>’;
}
echo “</div><!–custom-calendar-end–>”;

ざっと手直ししただけなので、細かい部分は面倒なので訂正しませんでした。
テーブルタグだと「次の月」と「前の月」がtdで隣り合わせになっていたので、「次の月」を一番下にもってきて移動。

(イメージ)

別サイトで使っているWordpressは複数のブログを扱っておらず、使い分けをする必要もないので、暫定的にコレを使用することにします。

時間があれば、これをプラグイン化する挑戦をしてみたいですね・・・・

コメントはまだありません