Compact Archives Widget for WordPress


Comparison between long and compact WordPress Archives WidgetThe standard Archives Widget looks like the image right here. It works well when only a few months are to be displayed. However, it fails if your blog’s history goes back a long way. Here is a  quick way to get down from 12 lines per year to 1, increasing usability and page size.

If you can log in to your WordPress web server, it is as easy as changing into the wordpress directory (e.g., /var/www/wordpress), downloading compact-archives.diff and executing

patch -p0 < compact-archives.diff

resulting in output like the following (the line and offset numbers will change between WordPress versions)

patching file wp-includes/general-template.php
Hunk #1 succeeded at 1403 (offset 135 lines).

Unfortunately, you have to repeat this after every WordPress upgrade. Is there an easy way to get this into a plugin or theme so this can be relaxed?

Here is the contents of compact-archives.diff; copy-pasting from here will likely result in whitespace or line break differences causing the patch to fail. (The code was designed to change as little from the existing wp_get_archives() function.)

Index: wp-includes/general-template.php
===================================================================
--- wp-includes/general-template.php    (revision 1921)
+++ wp-includes/general-template.php    (working copy)
@@ -1268,15 +1268,29 @@
         }
         if ( $results ) {
             $after = $r['after'];
+            $pyear = "XXXX";
             foreach ( (array) $results as $result ) {
-                $url = get_month_link( $result->year, $result->month );
-                /* translators: 1: month name, 2: 4-digit year */
-                $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
+                $y = sprintf("%04d", $result->year);
+                $m = sprintf("%02d", $result->month);
+                if ($y != $pyear) {
+                    if ($pyear != "XXXX") {
+                        $output .= "</li>";
+                    }
+                    $pyear = $y;
+                    $pmonth = "XX";
+                    $output .= '<li><b><a href="/' . $y . '/">' . $y . '</a>:</b> ';
+                }
+                if ($pmonth != "XX") {
+                    $output .= " | ";
+                }
+                $pmonth = $m;
+                $output .= '<a href="/' . $y . '/' . $m . '/">' . $m . '</a>';
+
                 if ( $r['show_post_count'] ) {
-                    $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
+                    $output .= '&nbsp;(' . $result->posts . ')';
                 }
-                $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
             }
+         $output .= "</li>";
         }
     } elseif ( 'yearly' == $r['type'] ) {
         $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
,

Let’s stay in touch!

Receive a mail whenever I publish a new post.

About 1-2 Mails per month, no Spam.

Follow me on the Fediverse

Web apps


Leave a Reply

Only people in my network can comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.