get_calenderの土日に色を付ける方法!
カテゴリー:WordPressget_calenderで呼び出したカレンダーの土日や平日などに色ってつけたくなったりしちゃいますよね!?
てことで思い立ったが吉日、つけちゃいましょうか!
以下のコードをfunctions.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")?>((<a href="[^"]+" title="[^"]+">)?' . $day . '(</a>)?)</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 );
これでカレンダーで使われているテーブルの列にクラスがつくのでそれをスタイルシートで色付けするだけ!
簡単ですね〜笑
ただこのコード、twentytenで使われているfunctions.phpようで作られているぽいのでだめだった場合はほかの方法を探すか、twentytenのfunctipns.phpを使うことをお勧めします