programing

워드프레스:클래스 셀렉터를 tags()에 추가하는 중; 출력

cafebook 2023. 3. 19. 18:29
반응형

워드프레스:클래스 셀렉터를 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>

이거 할 수 있어요?만약 그렇다면, 어떻게?감사합니다!

됐어요, 고마워요!제가 한 일은 다음과 같습니다.

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

또한 작업 부하가 너무 높을 수 있습니다.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');

이 코드는 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; ?>

대신 get_the_tags를 사용하여 for 루프를 수행하고 자체 마크업을 만듭니다.

언급URL : https://stackoverflow.com/questions/3314716/wordpress-adding-class-selectors-to-the-tags-output

반응형