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:

function my_get_archives_link($links) {
    $links = str_replace('</a>&nbsp;(', '</a> <span>(', $links);
    $links = str_replace(')', ')</span>', $links);
    return $links;
add_filter('get_archives_link', 'my_get_archives_link');
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.

  • those filters are specifically for those functions, you would need to provide more information as to what function is being used which outputs the author post count in the context you are targeting. the html by itself is not sufficient to determine this. – majick Sep 13 '17 at 13:02

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.

  • 1
    Sorry for the delay in reply - I replied last week to thank you but it must have made an error as its not showing here. Thanks so much for this code, that works like a charm. Excellent work – user3643520 Sep 21 '17 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.