How to add a class to anchor tags using the get_the_tag_list(); function?

admin2025-06-06  3

This question already has answers here: How do I change the output of get_the_tag_list()? (3 answers) Closed 6 years ago.

What's the easiest way to add a class to the anchor tag using the get_the_tag_list() function?

For example by adding;

if(get_the_tag_list()) {
echo get_the_tag_list('<ul><li>','</li><li>','</li></ul>');
}

Will return something like this;

<ul>
<li><a href="tag1">Tag 1</a></li>
<li><a href="tag2">Tag 2</a></li>
 ... 
</ul>

I just need something to add a custom class to the anchor. Thank you.

This question already has answers here: How do I change the output of get_the_tag_list()? (3 answers) Closed 6 years ago.

What's the easiest way to add a class to the anchor tag using the get_the_tag_list() function?

For example by adding;

if(get_the_tag_list()) {
echo get_the_tag_list('<ul><li>','</li><li>','</li></ul>');
}

Will return something like this;

<ul>
<li><a href="tag1">Tag 1</a></li>
<li><a href="tag2">Tag 2</a></li>
 ... 
</ul>

I just need something to add a custom class to the anchor. Thank you.

Share Improve this question asked Oct 29, 2018 at 20:26 Foxtrail815Foxtrail815 192 bronze badges 1
  • Check this wordpress.stackexchange/questions/32234/… – Michael Commented Oct 29, 2018 at 21:03
Add a comment  | 

1 Answer 1

Reset to default 1

I finally found the solution using a filter:

add_filter( "term_links-post_tag", 'add_tag_class');

function add_tag_class($links) {
return str_replace('<a href="', '<a class="myclass" href="', $links);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749223070a317394.html

最新回复(0)