2690

WordPressの年アーカイブリストで、年の追加、投稿数をaタグ内に入れるコード

simplu27 / Pixabay

WordPressの月アーカイブだと投稿数が少なすぎる場合、年アーカイブリストを使うのですが、いろいろ使いづらい部分を改良するコードを紹介します。

不便な所

コード

functions.phpに以下のコードをコピペ

function filter_to_archives_link( $link_html, $url, $text, $format, $before, $after ) {
  if ( 'html' === $format ) {
      $output = preg_replace('/.*((d+))/','$1',$after);
    $link_html = "
  • $before$text"."年 "."$output
  • n"; } return $link_html; } add_filter( 'get_archives_link', 'filter_to_archives_link', 10, 6 );

    表示該当箇所にwp_get_archiveを追加

      'yearly', 'limit' => '5', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => true, 'echo' => 1, 'order' => 'DESC', 'post_type' => 'post' ); wp_get_archives( $args ); ?>
    モバイルバージョンを終了