この記事は最後に更新してから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_save_post_revision(2024年1月10日 更新)
int | WP_Error | void wp_save_post_revision( int $post_id )
現状の投稿のリビジョンを作成する。
get_the_author_posts_link(2019年8月21日 更新)
string get_the_author_posts_link( )
投稿者アーカイブページのリンクを取得する。
get_year_link(2012年2月2日 更新)
string get_year_link( mixed $year )
年アーカイブのURLを取得する。
register_taxonomy(2022年6月1日 更新)
WP_Taxonomy | WP_Error register_taxonomy( string $taxonomy, array | string $object_type, array | string $args = array() )
タクソノミーを登録する。
wp_add_object_terms(2013年8月6日 更新)
mixed wp_add_object_terms( int $object_id, mixed $terms, mixed $taxonomy )
投稿情報などにタクソノミーを追加する。