Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the following code to generate tags

<?php if( has_tag() ): ?>
<?php echo '<div id="tagWrapper">'?><p>Tags</p><?php the_tags('<ul class="tags"><li class="tag-body olive">','</li><li>','</li></ul>'); ?><?php echo '</div>' ?>
<?php endif; ?>

It's output is an unordered list with simple <a> links. I need to apply a class to these links class="tag-body olive

What file is generating these tags I looked in my themes functions.php

Thanks

share|improve this question
    
Edited my answer, that's the code you need to use, not the one earlier. –  its_me Jun 30 '12 at 13:01
    
I ended up applying the style to the li tag –  Anagio Jun 30 '12 at 13:02
add comment

2 Answers 2

up vote 3 down vote accepted

The function get_the_tags(); is probably what you are looking for. The following code displays a list of tags with links to each one and a specific class for each tag:

<?php
    $tags = get_the_tags();
    $html = '<div class="post_tags">';
    foreach ($tags as $tag){
        $tag_link = get_tag_link($tag->term_id);

        $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
        $html .= "{$tag->name}</a>";
    }
    $html .= '</div>';
    echo $html;
?>

Now, using that logic, I've modified your code to do what you want:

<?php if( has_tag() ) { ?>
    <div id="tagWrapper">

        <p>Tags</p>

        <?php
        $tags = get_the_tags();
        $html = '<ul class="tags">';
        foreach ($tags as $tag){
            $tag_link = get_tag_link($tag->term_id);
            $html .= "<li class="tag-body olive"><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
            $html .= "{$tag->name}</a></li>";
        }
        $html .= '</ul>';
        echo $html;
        ?>

    </div>
<?php } ?>

And the output in case it helps (although untested) should look like this:

<div id="tagWrapper">
    <p>Tags</p>
    <ul class="tags">
        <li class="tag-body olive">
            <a href='http://example.com/tag/technology/' title='Technology Tag' class='technology'>Technology</a>
        </li>

        <li class="tag-body olive">
            <a href='http://example.com/tag/gadgets/' title='Gadgets Tag' class='gadgets'>Gadgets</a>
        </li>

        <li class="tag-body olive">
            <a href='http://example.com/tag/mobile/' title='Mobile Tag' class='mobile'>Mobile</a>
        </li>
    </ul>
</div>

SOURCE: WordPress Codex Function Reference for get_the_tags();

share|improve this answer
    
just be aware that get_tags() will get the tags from the site; while get_the_tags() would get tags from the post - codex.wordpress.org/Function_Reference/get_the_tags –  Michael Jun 30 '12 at 12:54
    
@Michael Thanks for spotting the mistake. Corrected! Oops moment! –  its_me Jun 30 '12 at 13:00
add comment

Take a look at this filter: http://wordpress.org/support/topic/the_tags-and-add_filter - play with the output to add your class.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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