WordPressテーマのリニューアル中につまずいたので備忘録として残します。
wp_get_archives([ 'show_post_count' => 1,]);
アーカイブ一覧をそのアーカイブに含まれる投稿の数と一緒に取得します。
上記コードだと以下のように出力されます。
<ul> <li><a href="https://sample.jp/2022/01/">2022年1月</a> (2)</li> <li><a href="https://sample.jp/2022/12/">2021年12月</a> (2)</li></ul>
私は(件数)
の表示はあまり好きではないので括弧を削除します。
function format_wp_get_archives($link_html, $url, $text, $format, $before, $after){ $after = str_replace(' (', '', $after); // 括弧開きを削除 $after = str_replace(')', '', $after); // 括弧閉じを削除
if ($format === 'html') { $link_html = ' <li class="list-group-item d-flex justify-content-between align-items-start"> <div class="ms-2 me-auto"> <a href="' . $url . '" class="link-dark">' . $text . '</a> </div> <span class="badge bg-primary rounded-pill">' . $after . '</span> </li> '; }
return $link_html;}
add_filter('get_archives_link', 'format_wp_get_archives', 10, 6);
str_replace
で括弧をから文字に置換するだけで完了です。