この記事は最後に更新してから1年以上経過しています。
説明
ウィジェットベースでテーマを構築していてカレンダーを表示してみた。標準では、前月や次月のリンク先はそれぞれの月のアーカイブページとなっているのだが、記事を探すだけであればカレンダー部分だけを更新するほうが便利なのではと考え、そのアイデアを実装してみた。実装は「カレンダーウィジェットのリンク部分の変更」、「リンククリック時のJavaScript関数の追加」、「更新するカレンダー部分のレスポンス対応」の3パートに分かれる。 まずはカレンダーウィジェットの前月や次月のリンクをJavaScriptで対応する部分。これはwidgets_initアクションで行うのがいいと思い、functions.phpに次のように追加。
add_action( 'widgets_init', 'mytheme_widgets_init' );
function mytheme_widgets_init() {
add_filter( 'get_calendar', 'mytheme_get_calendar' );
}
function mytheme_get_calendar( $out ) {
return str_replace( ' id="next"><a ', ' id="next"><a onclick="update_calendar(this.href); return false;" ',
str_replace( ' id="prev"><a ', ' id="prev"><a onclick="update_calendar(this.href); return false;" ', $out ) );
}function update_calendar( dateurl ) {
$.ajax( {
url: dateurl+'?ajax',
dataType: 'html',
success: function( data ) {
$( '#calendar_wrap' ).html( data );
}
} );
}if ( is_month() && array_key_exists( 'ajax', $_GET ) ) {
get_calendar();
exit;
}最終更新 : 2011年08月22日 14:09
関連
お勧め
wp_update_user_counts(2022年6月1日 更新)
bool wp_update_user_count( int $network_id = null )
ユーザー数を更新する。
wp_hash(2025年4月17日 更新)
string wp_hash( string $data [ , string $scheme = 'auth' [ , string $algo = 'md5' ] ] )
ハッシュを生成する。
wp_count_attachments(2013年10月31日 更新)
object wp_count_attachments( [ string $mime_type = '' ] )
添付ファイル数を取得する。
locate_block_template(2021年7月26日 更新)
string locate_block_template( string $template, string $type, array $templates )
ブロックテンプレートを探す。
load_child_theme_textdomain(2018年5月27日 更新)
bool load_child_theme_textdomain( string $domain [ , mixed $path = false ] )
子テーマ用の国際化用ファイル(MOファイル)をロードする。