How to Clean Up and Shorten a Long WordPress Archive List
After importing some old posts, my archive list suddenly spanned over a decade. Because it displays every month like "July 2022" stacked vertically, it became way too long and honestly looked pretty messy. When I looked online, I found a few different approaches, but none of them felt like a definitive fix—and there doesn't seem to be a standard best practice. Right now, I'm just having fun working through it through trial and error.
The Options
- Use the default WordPress Archive block settings, which let you choose between yearly, monthly, weekly, or daily displays.
- Install the "Compact Archives" plugin.
- Modify the template's PHP so that archive retrieval uses "months" for recent posts and "years" for older ones.
- Modify functions.php to apply a filter to the archive query, grouping months under their respective years.
I listed those in order of quickness, but during my research, I actually discovered them in the order of 3 -> 4 -> 2 -> 1. That naturally got me greedy about wanting a customized display and tinkering with PHP.
Ultimately, what I want to achieve is a mix of 3 and 4: grouping months by year for recent posts, while displaying older posts purely by the year.
I'll be updating this page as I research, test, and implement this goal.
Option 1: Fine-tuning the Block Settings
You can configure block displays for sidebars and footers under Appearance > Widgets. There's an archive display option there where you can tweak settings like "Display as dropdown" or "Show post counts." However, it's a bit clunky and not quite what I want. Ideally, I want recent posts linked by month and older posts grouped broadly by year. Still, playing around with it was a good way to learn more about blocks.


Option 2: Installing the "Compact Archives" Plugin
This might be the fastest route, but it formats everything uniformly as "2022:07 06 05." Having seen options 3 and 4, I really want that hybrid look. I'll give those a serious shot first, and fall back on the plugin if things get too messy.

"jQuery Archive List Widget"Apparently, this is another alternative worth noting.
Option 3: Modifying the Display PHP (Using Conditionals for Months vs. Years)
Reference site:
https://qiita.com/noqua/items/02307a2f45f61c882714
This one is pretty nice.
The Catch
There are a few drawbacks. The guide mentioned editing files like sidebar.php, but it wasn't entirely clear where to make the changes. For now, I just modified the sidebar section within the Lightning theme I've been customizing, and it started displaying correctly.
The issue with this method is that you have to make changes in two places: wherever the Archive block is used, and anywhere it isn't. Plus, I've customized the Lightning theme so much that I'm getting nervous about theme updates wiping out my changes. Option 4, on the other hand, involves adding code to functions.php, which feels much safer. Furthermore, since it hooks directly into the core archive retrieval function, it applies uniformly whether you're using blocks or site PHP.
So I'm testing this out now, and I suspect the final solution will be a hybrid of 3 and 4. It would be awesome if I could just build a small auxiliary plugin for this.
By the way, while doing this, I ran into another mystery that had been bugging me: the "Lightning theme displays categories twice" issue. I'll write an article explaining that and its solution separately. Tinkering with this has definitely given me a better grasp of how WordPress works under the hood.
Option 4: Adding a Filter to the Archive Retrieval Function
Adding get_archives_link() Filter Code to functions.php
I followed this site as a reference:
https://usortblog.com/widgetarchiveshorter/
By writing custom code in functions.php, you can filter the HTML string output generated by existing code and modify it.
The Lightning theme actually has two functions.php files, and changes to either seem to work. There's one under the `_g3` subfolder and one directly under the main Lightning folder. I ended up editing the upper-level one.
Issues Encountered
The snippet wasn't quite plug-and-play, so I had to make a few tweaks.
1. The link URLs were hardcoded as "/site-url/year/month/"
My site uses "/site-url/date/year/month", so the code didn't work out of the box. I could have just added "date" manually, but I figured I should just use the original URLs properly. After digging into PHP, I settled on using regular expressions to extract paths enclosed in single quotes. I always forget regex syntax, so I end up having to look it up every single time.
2. This filter gets called for yearly views as well as monthly views, which resulted in blank outputs for yearly listings.
I wasn't using this on the live server yet, but I noticed it on my local test server where I had configured items older than 12 months to display by year only. There might be other edge cases, but I updated it to only run the logic when the string contains "month," and return the original string otherwise. (Note: It also gets called for "weeks" and "days.")
3. Years with no posts in December wouldn't display their year heading, because the script was using December as the key. I realized this later and fixed it by tracking whether it was the first entry for that year using a flag.
4. If the current year has posts but the current month has none, the year fails to display.
5. Extra HTML tags were showing up.
Current Code
/* ウィジェットで[show]されるアーカイブを短く[show]させます
* 1.リンクが/サイトurl/2022/07というように固定される。リンクは設定次第で変わるので、リンクはそのまま使いたい。 →解決
* 2.月や週や日で指定された場合の考慮がない →解決
* 3.12月に記事がない年は年[show]がされない →解決
* 4.今月記事を書いてないと年[show]がされない →解決
* 5.今年の記事しかなく、今月の記事がない場合、2回アーカイブを呼ばれると年が[show]されない。→未解決
* 月[show]の後不要なhtmlタグが出ている「</a></li>」 →解決
* もうガンガン作り変えます
*/
function my_archives_link($link_html){
$currentMonth = date('n');
$currentYear = date('Y');
//年指定の場合は何もしない
if(strpos($link_html,'月') != true){
return $link_html;
}
//年/月/(日でもOK?)の場合
// アーカイブの年月HTMLを編集
$ym = explode('年', $link_html); //ym[0]=<li><a href='http://localhost/?m=202206'>2022 ym[1]=6月</a></li>
$monthArray = explode('月', $ym[1]); //$monthArray[0]=6 [1]=</a></li>
$month = $monthArray[0]; //=6
$year = intval(strip_tags($ym[0])); //2022 remove php html tags
//$linkMonth = substr('0'.$month, -2); //0+月の後ろ2文字 012 → 12, 07→07、2桁にしている いらない
//リンク先
preg_match_all('/'.+?'/', $ym[0], $urlArray);//正規表現で''で囲まれたURLを抽出
$url = $urlArray[0][0];
//echo '$urlEX =';echo $url;var_dump ($linkMonthArray); //for debug
$linkMonth = '<a href='.$url.' style="white-space: nowrap;">'.$month.'月 </a>';
$linkYear = ''; //条件により生成する
$formatYear = '<span style="font-weight:bold;">%s年</span><br />';
//$linkYearを条件により作成し、$_SESSION['printyear']を今処理中の年を入れる。これは1年に1度しか処理させないため
//今まで出力していた年とは違う場合=初めてその年を出力するとき
if ($_SESSION['printyear']!=$year) { //初期値は""空白である。2回目以降呼び出されると最後に処理した年となる
//※今年の記事しかない場合に2回目に呼ばれると$_SESSION['printyear']が今年から更新されないので引っかからない
$linkYear = sprintf($formatYear, $year); //1年目「年+改行」
$_SESSION['printyear']=$year; //[年+改行]出力した年を記録
if($currentYear != $year){ //今年ではない場合
$linkYear = '<br />'.$linkYear; //さらに頭に[改行]を加える「[改行]+年+[改行]」
}
}
//2回目に呼ばれた時の対策1.:今月だったら間違いなし。不十分。今月書いてなかったらダメ
else if (($currentMonth == $month) AND ($currentYear == $year)){
$linkYear = sprintf($formatYear, $year); //1年目「年+改行」
$_SESSION['printyear']=$year; //[年+改行]出力した年を記録
}
//そうでない場合は初期値$linkYear = ''
//'[改行]or[無]2020[改行]' or '無' + '<a href='.$url.' style="white-space: nowrap;">6月</a>'
return $linkYear.$linkMonth; //$link_htmlを$linkYear + $linkMonthに加工して返す
}
add_filter('get_archives_link', 'my_archives_link');
Resulting Display
This is the result of combining 3 and 4. Thanks to #3, the 12 most recent months are shown individually, while anything older is grouped by year. Furthermore, for the monthly view, instead of redundantly showing "July 2022 [line break] June 2022", filter #4 ensures the year like 2022 only appears once as a header, followed by a clean list of months.

This is what it looks like after resolving issues 1 and 2.
I felt pretty good about it at this point, but I later realized that some older articles lacked posts in December. Disabling the workaround in #3 made them visible, but that pattern still wasn't rendering the year headings correctly.
After sorting out the issues with steps 3 and 4:
5. If there are posts from this year, but none from this current month, and the archive function is called twice, the year fails to display. -> Unresolved
Since calling the function twice is avoidable, maybe I should just call it a day? Lightning calls it twice by default, so be sure to check out my next troubleshooting post for the fix.
Editing PHP Code
Up until now, I've been SSHing into the server via shell and editing code with Emacs—specifically, testing things in a local environment and uploading them to the server once they were done.
Turns out, you can easily do this right inside WordPress too.
If you go to Appearance > Theme File Editor, you can edit PHP and other theme files. The best part is that it checks your syntax for errors when you save. That said, sometimes Japanese comment encodings get messed up and trigger errors anyway. Still, this method is super convenient for copy-pasting other people's code or making quick tweaks.


