2

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

  • 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
3

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();

0

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

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.