カレンダーの土、日の色を変える[WordPress]
新しめのバージョンのWordpressでカレンダーの土、日の色を変える方法。(Wordpress3.1以降で確認。それ以前のバージョンでもいけるかも)
2013年11月28日追記
プラグインを導入して有効化するだけという方法を発見しました。
カレンダーの土、日、祝日に色を付ける[WordPress]
この記事はfunction.phpにコードを書き足す方法です。
まえがき
WordPressいじってて絶対思うことの3本指に入るかどうかは知りませんが、
絶対思うと思います。
土日の色変えてーって。
WordPressのカレンダーは曜日別にclassとか付かないんです。
デフォでつけといてくださいよ~って感じですよね。
でも文句は言えませんよね。
解決策なんですが
function.phpにちょっとコードを追加するとカレンダーに魔法が掛かります。
土日だけじゃなく月~日までそれぞれclassがつきます。
因みにバージョンはWordpress3.1以降で確認してます。
それ以前でもいけるかも!?
function.phpにコードを追加する
function.phpに以下のコードを追加
//カレンダー土日クラス
function add_week_class2calendar( $calendar_output ) {
$week_map = array(
'mon' => '月曜日',
'tue' => '火曜日',
'wed' => '水曜日',
'thu' => '木曜日',
'fri' => '金曜日',
'sat' => '土曜日',
'sun' => '日曜日',
);
$regex = '/<th scope="col" title="([^"]+?)"/';
$num = preg_match_all( $regex, $calendar_output, $m );
if ( $num ) {
$replace = array();
for ( $i = 0; $i < $num; $i++ ) {
$replace[$i] = '<th scope="col" class="' . array_search( $m[1][$i], $week_map ) . '" title="' . $m[1][$i] . '"';
}
$calendar_output = str_replace( $m[0], $replace, $calendar_output );
}
return $calendar_output;
}
add_filter( 'get_calendar', 'add_week_class2calendar' );
function add_week_classes2calendar( $calendar_output ) {
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
if ( isset($_GET['w']) )
$w = ''.intval($_GET['w']);
// Let's figure out when we are
if ( !empty($monthnum) && !empty($year) ) {
$thismonth = ''.zeroise(intval($monthnum), 2);
$thisyear = ''.intval($year);
} elseif ( !empty($w) ) {
// We need to get the month from MySQL
$thisyear = ''.intval(substr($m, 0, 4));
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
} elseif ( !empty($m) ) {
$thisyear = ''.intval(substr($m, 0, 4));
if ( strlen($m) < 6 )
$thismonth = '01';
else
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
} else {
$thisyear = gmdate('Y', current_time('timestamp'));
$thismonth = gmdate('m', current_time('timestamp'));
}
$jp_holidays = get_option( 'jp_holidays' );
if ( ( ! $jp_holidays || !isset( $jp_holidays[$thisyear . $thismonth] ) || $jp_holidays[$thisyear . $thismonth]['expire'] < time() ) && $thisyear >= 2000 ) {
$holiday_api = 'http://www.finds.jp/ws/calendar.php?php&y=' . $thisyear . '&m=' . $thismonth . '&t=h&l=2';
$ch = curl_init( $holiday_api );
curl_setopt( $ch, CURLOPT_FAILONERROR, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 5 );
$source = curl_exec( $ch );
curl_close( $ch );
if ( $source ) {
$results = maybe_unserialize( $source );
if ( isset( $results['status'] ) && $results['status'] == 200 ) {
if ( ! is_array( $jp_holidays ) ) {
$jp_holidays = array();
}
$jp_holidays[$thisyear . $thismonth] = array();
if ( isset( $results['result']['day'] ) ) {
foreach ( $results['result']['day'] as $hday ) {
$jp_holidays[$thisyear . $thismonth][$hday['mday']] = array( 'type' => $hday['htype'], 'name' => $hday['hname'] );
}
$jp_holidays[$thisyear . $thismonth]['expire'] = time() + 365 * 24 * 3600;
}
update_option( 'jp_holidays', $jp_holidays );
}
}
}
$yar = (int)$thisyear;
$mon = (int)$thismonth;
$day = 1;
$regex = array();
while( checkdate( $mon, $day, $yar ) ) {
$classes = array();
$wday = date( 'w', strtotime( sprintf( '%04d-%02d-%02d', $yar, $mon, $day ) ) );
switch ( $wday ) {
case 0 :
$classes[] = 'sun';
break;
case 6 :
$classes[] = 'sat';
break;
default :
}
if ( $jp_holidays && is_array( $jp_holidays ) && count( $jp_holidays[$thisyear . $thismonth] ) && isset( $jp_holidays[$thisyear . $thismonth][$day] ) ) {
$classes[] = 'holiday';
}
$class = '';
if ( count( $classes ) ) {
$class = ' class="' . implode( ' ', $classes ) . '"';
}
if ( $class ) {
$regex['|<td( id="today")?>(()?' . $day . '()?)</td>|'] = '<td$1' . $class . '>$2</td>';
}
$day++;
}
$calendar_output = preg_replace( array_keys( $regex ), $regex, $calendar_output );
return $calendar_output;
}
add_filter( 'get_calendar', 'add_week_classes2calendar', 0 );
カレンダーを出力したいところで
<?php get_calendar(); ?>
これで曜日別にclassが付いたカレンダーが出力されるはずです。
参考サイト
なんと参考サイトを忘れてしまったという失態。
ごめんなさい。
その辺で首吊ってきます。
チャンチャン。


![マルチサイト化した複数のブログの新着記事を表示する[WordPress]](https://kotori-blog.com/wordpress/wp-content/uploads/wordpress-logo-notext-rgb-31-140x140.gif)
![カスタムフィールドをサイト内検索の対象にする「Search Everything」[WordPress]](https://kotori-blog.com/wordpress/wp-content/uploads/thumg-140x140.jpg)