Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: Adding class selectors to the_tags(); output

How do I get the_tags() to output each tag so that it comes assigned with a unique class selector? So for example: the_tags() currently outputs something like this:

<a href="http://myblog.com/tag/kittens" rel="tag">kittens</a>

However, I'd like to output something like this:

<a href="http://myblog.com/tag/kittens" rel="tag" class="tag-kittens">kittens</a>

Is it possible to do this? If so, how? Thanks!

like image 360
Marc P Avatar asked Jul 23 '10 01:07

Marc P


People also ask

How do I add a class to a tag in WordPress?

Because body classes are theme specific, you would need to add the following code to your theme's functions. php file. add_filter( 'body_class' , 'my_class_names' ); The above code will add a class “wpb-class” to the body tag on every page on your website.

How do I show tags on WordPress?

Upon activation, you need to visit Appearance » Widgets page and add 'Tag Cloud (Simple Tags)' widget to the sidebar. The widget will expand, and you will be able to see its settings. Here you can select the number of tags you want to display, font sizes, colors, etc.

Which of the following is a function of tags in WordPress?

WordPress tags is one of the tools you can use to group your posts, based on similar details. Usually, tags are located under a post or in the sidebar. When a visitor clicks a particular tag, WordPress will open an archive page (tag page) – indexing all the posts and custom post types that have the same tags.


2 Answers

Also you can overload working of get_the_tags(); function. Just add next code to your functions.php theme file:

// add custom class to tag
function add_class_the_tags($html){
    $postid = get_the_ID();
    $html = str_replace('<a','<a class="class-name"',$html);
    return $html;
}
add_filter('the_tags','add_class_the_tags');
like image 181
alx lark Avatar answered Nov 12 '22 21:11

alx lark


this code from www.lawturn.com

/* SlSlib tags add class */
<?php if(has_tag()) : ?>

    <?php
    $tags = get_the_tags(get_the_ID());
      foreach($tags as $tag){
        echo '<a href="'.get_tag_link($tag->term_id).'" rel="tag" class="tag-'.$tag->name.'">'.$tag->name.'</a>';
    } ?>

<?php endif; ?>
like image 37
Gnana lingam Avatar answered Nov 12 '22 21:11

Gnana lingam