2010-07-23 3 views
7

고유 한 클래스 선택기가 할당되도록 각 태그를 출력하도록 the_tags()를 얻으려면 어떻게해야합니까? 그래서 예를 들면 :Wordpress : the_tags()에 클래스 선택기를 추가; 출력

<a href="http://myblog.com/tag/kittens" rel="tag" class="tag-kittens">kittens</a> 

이 가능이 작업을 수행 :이 같은 출력 뭔가를하고 싶은, 그러나

<a href="http://myblog.com/tag/kittens" rel="tag">kittens</a> 

: the_tags() 현재이 같은 출력? 그렇다면 어떻게? 감사!

답변

1

대신 get_the_tags을 사용하고 for 루프를 실행하고 자신 만의 마크 업을 만드십시오.

11

감사합니다. 이것은 내가 한 것입니다 :

<?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>'; 
    } 
} 
?> 
+4

에서 당신은 내 대답을 표시 할 수 있습니다 허용하거나 검색 할 때 당신이 그렇게 다른 사람들이 도움이 될로 같은 문제. 어떻게 SO가 FAQ에서 작동하는지 읽을 수 있습니다. http://stackoverflow.com/faq – GmonC

3

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'); 
+0

니스. 나는 해결책을 좋아한다. – somethingnow

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; ?> 
관련 문제