Using WordPress ‘get_archives_link’ PHP filter

The get_archives_link WordPress PHP filter allows you to modify the content of an archive link.

Usage

add_filter('get_archives_link', 'your_custom_function', 10, 7);

function your_custom_function($link_html, $url, $text, $format, $before, $after, $selected) {
    // your custom code here
    return $link_html;
}

Parameters

  • $link_html (string) – The archive HTML link content.
  • $url (string) – URL to the archive.
  • $text (string) – Archive text description.
  • $format (string) – Link format. Can be ‘link’, ‘option’, ‘html’, or custom.
  • $before (string) – Content to prepend to the description.
  • $after (string) – Content to append to the description.
  • $selected (bool) – True if the current page is the selected archive.

More information

See WordPress Developer Resources: get_archives_link

Examples

Modify the archive link by adding a custom class for styling purposes.

add_filter('get_archives_link', 'add_custom_class_to_archive_links', 10, 7);

function add_custom_class_to_archive_links($link_html, $url, $text, $format, $before, $after, $selected) {
    $link_html = str_replace('<a', '<a class="custom-class"', $link_html);
    return $link_html;
}

Add an icon before the archive text

Prepend an icon to the archive link description.

add_filter('get_archives_link', 'add_icon_before_archive_text', 10, 7);

function add_icon_before_archive_text($link_html, $url, $text, $format, $before, $after, $selected) {
    $icon = '<i class="archive-icon"></i>';
    $link_html = str_replace($text, $icon . $text, $link_html);
    return $link_html;
}

Modify the archive link format to display as an unordered list.

add_filter('get_archives_link', 'change_archive_link_format', 10, 7);

function change_archive_link_format($link_html, $url, $text, $format, $before, $after, $selected) {
    $link_html = '<li>' . $link_html . '</li>';
    return $link_html;
}

Highlight the current archive page

Apply custom styling to the selected archive link.

add_filter('get_archives_link', 'highlight_current_archive_page', 10, 7);

function highlight_current_archive_page($link_html, $url, $text, $format, $before, $after, $selected) {
    if ($selected) {
        $link_html = str_replace('<a', '<a style="font-weight: bold;"', $link_html);
    }
    return $link_html;
}

Modify the archive text description

Change the archive text description to display the number of posts.

add_filter('get_archives_link', 'modify_archive_text_description', 10, 7);

function modify_archive_text_description($link_html, $url, $text, $format, $before, $after, $selected) {
    $new_text = $text . ' (10 posts)';
    $link_html = str_replace($text, $new_text, $link_html);
    return $link_html; 
}