Apply class to the a tag link generated in post tag links

I'm using the following code to generate tags<?php if( has_tag() ): ?><?php echo '<div id="t

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

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 asked Jun 30, 2012 at 9:56 AnagioAnagio 9533 gold badges20 silver badges47 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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/tag/technology/' title='Technology Tag' class='technology'>Technology</a>
        </li>

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

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

SOURCE: WordPress Codex Function Reference for get_the_tags();

/****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*****/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264259a4619356.html

相关推荐

  • Apply class to the a tag link generated in post tag links

    I'm using the following code to generate tags<?php if( has_tag() ): ?><?php echo '<div id="t

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信