How do I change the output of get_the_tag_list()?

So, from the get_the_tag_list() Codex entry:

  • $before
    • (string) (optional) Leading text.
    • Default: ‘Tags: ‘
  • $sep
    • (string) (optional) String to separate tags.
    • Default: ‘, ‘
  • $after
    • (string) (optional) Trailing text.
    • Default: None

Since you’re using this example:

get_the_tag_list('<ul><li>','</li><li>','</li></ul>');

…edited to save space…

Modify it as such:

get_the_tag_list( '<ul><li><span>', '</span></li><li><span>', '</span></li></ul>' );

…should get you what you’re after.

EDIT

I think you misunderstood my question. The span is supposed to be in the anchor tags. Your method would output <li><span><a></a></span></li>

Sorry about that; I misread the question.

This gets a bit trickier, but is entirely possible. The get_the_tag_list() template tag uses get_the_term_list(), which has a filter for the term links, called term_links-$taxonomy (which for tags would be, term_links-post_tag.

So, you could write a filter:

function mytheme_filter_post_tag_term_links( $term_links ) {
    $wrapped_term_links = array();
    foreach ( $term_links as $term_link ) {
        $wrapped_term_links[] = '<span>' . $term_link . '</span>';
    }
    return $wrapped_term_links;
}
add_filter( 'term_links-post_tag', 'mytheme_filter_post_tag_term_links' );

Note: this will apply to every use of get_the_term_list() that outputs post tags. Caveat emptor.

Leave a Comment