この記事は最後に更新してから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_add_object_terms(2013年8月6日 更新)
mixed wp_add_object_terms( int $object_id, mixed $terms, mixed $taxonomy )
投稿情報などにタクソノミーを追加する。
sanitize_key(2018年5月27日 更新)
string sanitize_key( string $key )
サイト内識別子向けにサニタイズする。
get_default_comment_status(2018年5月27日 更新)
string get_default_comment_status( [ string $post_type = 'post' [ , string $comment_type = 'comment' ] ] )
投稿タイプのコメントステータスを取得する。
wp_slash_strings_only(2020年12月17日 更新)
mixed wp_slash_strings_only( $value )
配列またはオブジェクト内の文字列をバックスラッシュでエスケープする。
url_shorten(2018年5月27日 更新)
string url_shorten( string $url [ , int $length = 35 ] )
(表示向けに)URLを短縮する。