先週やった内容を、試しにWordPressプラグインでやってみました。
以下、ネットで情報を漁りながら、自分が作った方法です。
1.wp-content/plugin/wareki というフォルダを作成し、その中に wareki.php を作成する。
2.wareki.php 内のコードは以下のようにして、保存。
<?php /* Plugin Name: wareki Plugin URI: http://www.example.com/plugin Description: テスト用に作った、西暦を和暦に変更するプラグイン Author: fukapu Version: 0.1 Author URI: http://wordpress.nakweb.com */ if ( ! function_exists( 'twentytwelve_entry_meta' ) ) : /** * Prints HTML with meta information for current post: categories, tags, permalink, author, and date. * * Create your own twentytwelve_entry_meta() to override in a child theme. * * @since Twenty Twelve 1.0 */ function twentytwelve_entry_meta() { // Translators: used between list items, there is a space after the comma. $categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) ); // Translators: used between list items, there is a space after the comma. $tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) ); // 日付を取得し、和暦にする処理 $ymd = get_the_date('Y/n/j'); $wareki = wareki($ymd); $date = $wareki; /* $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>', esc_url( get_permalink() ), esc_attr( get_the_time() ), esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ) ); */ $author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>', esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ), get_the_author() ); // Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name. if ( $tag_list ) { $utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' ); } elseif ( $categories_list ) { $utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' ); } else { $utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' ); } // Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name. if ( $tag_list ) { $utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' ); } elseif ( $categories_list ) { $utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' ); } else { $utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' ); } printf( $utility_text, $categories_list, $tag_list, $date, $author ); } endif; function wareki($ymd) { list($y,$m,$d) = explode("/",$ymd); $m = str_pad($m,2,0,STR_PAD_LEFT); $d = str_pad($d,2,0,STR_PAD_LEFT); $ymd = $y.$m.$d; if ($ymd <= "19120729") { $gg = "明治"; $yy = $y - 1867; } elseif ($ymd >= "19120730" && $ymd <= "19261224") { $gg = "大正"; $yy = $y - 1911; } elseif ($ymd >= "19261225" && $ymd <= "19890107") { $gg = "昭和"; $yy = $y - 1925; } elseif ($ymd >= "19890108") { $gg = "平成"; $yy = $y - 1988; } $m = ereg_replace("^0+", "", $m); $d = ereg_replace("^0+", "", $d); $wareki = "{$gg}{$yy}年{$m}月{$d}日"; return $wareki; }
3.WordPressの管理画面にログインし、プラグインメニューを開くと、「wareki」がインストール済みプラグインに並んでいるはずなので、有効化する。
4.フロント側で和暦表示になっているか確かめる。
試したいというチャレンジャーな方は、以下からダウンロードください。
wareki.zip