【问题标题】: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>';
      }
    }
    ?>
    

    【讨论】:

    • 您可以将我的答案标记为已接受或您的答案,以便其他人在搜索相同问题时受益。您可以在常见问题解答中阅读 SO 的工作原理。 stackoverflow.com/faq
    • 你能贴出解决这个问题的所有代码吗?
    【解决方案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; ?>
    

    【讨论】:

      【解决方案4】:

      改用get_the_tags,执行for循环并创建您自己的标记。

      【讨论】:

        猜你喜欢
        • 2012-01-27
        • 2016-02-08
        • 2022-06-28
        • 1970-01-01
        • 2018-06-27
        • 1970-01-01
        • 1970-01-01
        • 2020-02-04
        • 1970-01-01
        相关资源
        最近更新 更多