【问题标题】:How to hide certain tags from the_tags() in WordPress?如何在 WordPress 中隐藏 the_tags() 中的某些标签?
【发布时间】:2011-09-08 00:19:57
【问题描述】:

我需要为我的帖子分配一些标签(供外部使用),但我不希望它们显示在列出标签的任何地方。谁能给我一个例子来说明如何做到这一点?

【问题讨论】:

    标签: php wordpress tags


    【解决方案1】:

    这个问题很老了,但我遇到了这个需求,我找到了一个有趣的解决方案,我想分享。

    最好对标签应用过滤器,这样您就不必担心模板中显示标签的位置丢失了。

    function exclude_tags($tags) {
     foreach ($tags as $tag)
      switch ($tag->name) {
       case 'exclude-this-tag':
       case 'exclude-this-tag-too':
        break;
       default:
        $newtags[] = $tag;
     }
     return $newtags;
    }
    add_filter( 'get_the_tags', 'exclude_tags');
    

    【讨论】:

    • 对我来说是the_tags 过滤器。我的主题使用“the_tags”功能。
    【解决方案2】:

    在模板中使用 get_tags() 代替 the_tags()

     $tags = get_tags();
    
     foreach ($tags as $tag)
     {
       if($tag->name=='the tag i want gone') continue;// do this for every tag you want gone
       echo $tag->name.', ';  
     }
    

    【讨论】:

    • 行“echo $tag;”出现以下错误。可捕获的致命错误:stdClass 类的对象无法转换为字符串”。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多