この記事は最後に更新してから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
関連
お勧め
do_all_enclosures(2020年12月11日 更新)
void do_all_enclosures()
すべてのエンクロージャーを実行する。
get_term_feed_link(2012年2月23日 更新)
string get_term_feed_link( int $term_id [ , string $taxonomy = 'category' [ , string $feed = '' ] ] )
カテゴリーや投稿タグなどのタームフィードのURLを取得する。
get_available_post_mime_types(2023年11月15日 更新)
string[] get_available_post_mime_types( [ string $type = 'attachment' ] )
投稿タイプで利用可能なすべての投稿MIMEタイプを取得する。
is_registered_sidebar(2018年5月27日 更新)
bool is_registered_sidebar( mixed $sidebar_id )
サイドバーが登録されているか調べる。
wp_http_validate_url(2013年6月22日 更新)
mixed wp_http_validate_url( string $url )
安全なURLか調べる。