【问题标题】:Wordpress: Adding class selectors to the_tags(); outputWordpress:将类选择器添加到 the_tags();输出
【发布时间】:2010-07-23 01:20:37
【问题描述】:
如何让 the_tags() 输出每个标签,以便为其分配一个唯一的类选择器?例如:the_tags() 当前输出如下内容:
<a href="http://myblog.com/tag/kittens" rel="tag">kittens</a>
但是,我想输出如下内容:
<a href="http://myblog.com/tag/kittens" rel="tag" class="tag-kittens">kittens</a>
可以这样做吗?如果是这样,怎么做?谢谢!
【问题讨论】:
标签:
wordpress
class
function
selector
【解决方案1】:
成功了,谢谢!这就是我所做的:
<?php
$post_tags = get_the_tags();
if ($post_tags) {
foreach($post_tags as $tag) {
echo '<a href="'; echo bloginfo();
echo '/?tag=' . $tag->slug . '" class="' . $tag->slug . '">' . $tag->name . '</a>';
}
}
?>
【解决方案2】:
您还可以重载get_the_tags(); 函数的工作。
只需将下一个代码添加到您的 functions.php 主题文件:
// 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');
【解决方案3】:
此代码来自 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; ?>