3

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

2
  • Edited my answer, that's the code you need to use, not the one earlier.
    – its_me
    Commented Jun 30, 2012 at 13:01
  • I ended up applying the style to the li tag
    – Anagio
    Commented Jun 30, 2012 at 13:02

2 Answers 2

4

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

2
  • 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
    Commented Jun 30, 2012 at 12:54
  • @Michael Thanks for spotting the mistake. Corrected! Oops moment!
    – its_me
    Commented Jun 30, 2012 at 13:00
0
/****post-tag****/
function asr_tags() { 
    $asrtags =  get_the_tags();
    foreach($asrtags as $tag){
        $string .= '<span class="post-tag"><a class="p-tag" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n"   ;
    } 
    return $string;
} 
add_shortcode('asrtags' , 'asr_tags' );

/*****post-tag*****/
4
  • 2
    Can you please also add explanation what your code does? Commented Aug 5, 2019 at 8:57
  • @Innate where is the mistake? i use this in my wp function.php but when i chake my file manager then show in error log, buT still working this code. PHP Warning: Invalid argument supplied for foreach() in line 3
    – abdullahsk
    Commented Aug 6, 2019 at 18:58
  • Please use the edit link below your answer if you would like to add further information. Usually code only answer are not as well received without an explanation backing what the code does and how the code solves the original question.
    – Howdy_McGee
    Commented Aug 6, 2019 at 20:16
  • brother already solve the code
    – abdullahsk
    Commented Aug 6, 2019 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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