wp_get_archives: Put span inside anchor tags

Span outside anchor tags

I think you’re looking for the before and after arguments (PHP 5.4+):

wp_get_archives(
    [
        'before' => '<span>',
        'after'  => '</span>'
    ]
);

if you want to wrap the <span> tag around the <a> tag:

<li><span><a href="https://wordpress.stackexchange.com/questions/248311/xx">Link text</a></span></li>

Span inside anchor tags

If you want it inside the anchor tags:

<li><a href="https://wordpress.stackexchange.com/questions/248311/xx"><span>Link text</span></a></li>

then you could use the get_archives_link filter to reconstruct the links to your needs.

Modify the corresponding theme file with (PHP 5.4+):

// Add a custom filter
add_filter( 'get_archives_link', 'wpse_get_archives_link', 10, 6 );

// Archive
wp_get_archives(
    [
        'type'   => 'yearly', // For yearly archive
        'format' => 'html'    // This is actually a default setting
    ]
);  // EDIT the arguments to your needs (I'm not showing the <ul> part here)

// Remove the custom filter
remove_filter( 'get_archives_link', 'wpse_get_archives_link', 10, 6 );

where our filter callback is defined, in the functions.php file in the current theme directory, as:

function wpse_get_archives_link(  $link_html, $url, $text, $format, $before, $after )
{
    if( 'html' === $format )
         $link_html = "\t<li>$before<a href="https://wordpress.stackexchange.com/questions/248311/$url"><span>$text</span></a>$after</li>\n";

    return $link_html;
}

where we’ve added the span inside the anchor tag.

Leave a Comment