wrap a span tag around author's post count
-
28-02-2021 - |
Question
Is it possible to wrap a span tag around the author's post count so I can apply some css styling to the bracketed data for use in a sitemap?
On searching I found the solutions for Archives and Category per below:
//archives
function my_get_archives_link($links) {
$links = str_replace('</a> (', '</a> <span>(', $links);
$links = str_replace(')', ')</span>', $links);
return $links;
}
add_filter('get_archives_link', 'my_get_archives_link');
//categories
function my_wp_list_categories($links) {
$links = str_replace('</a> (', '</a> <span>(', $links);
$links = str_replace(')', ')</span>', $links);
return $links;
}
add_filter('wp_list_categories', 'my_wp_list_categories');
These two work really well for Archives and Category and I was hoping there would be something similar for the Author's post count.
The html is currently like this:
<li><a href="http://domain.com/author/user123/" title="Posts by user123">user123</a> (8)</li>
I would like it to be like this:
<li><a href="http://domain.com/author/user123/" title="Posts by user123">user123</a> <span>(8)</span></li>
Any ideas?
Thanks in advance.
Solution
It doesn't appear that wp_list_authors()
supplies an appropriate hook to modify anything unfortunately. The next best thing would be to just do a string replace and hope for the best :/
$start_wrapper = '<span class="author-post-count">'; // Set our wrapper start tag
$end_wrapper = '</span>'; // Set our wrapper end tag
$author_html = wp_list_authors( array( // Get Author HTML
'optioncount' => true,
'echo' => false, // Ensure we return and do not echo ( default is TRUE )
) );
$author_html = str_replace( '</a> (', "</a> {$start_wrapper}(", $author_html );
$author_html = str_replace( '</li>', "{$end_wrapper}</li>", $author_html );
echo $author_html;
The above will attempt to replace around the anchor tag HTML so you have a proper wrapper. At which point you can echo or append $author_html
where you see fit.