【问题标题】:wordpress - change the order of tags from the_tags()wordpress - 从 the_tags() 更改标签的顺序
【发布时间】:2011-04-22 18:54:34
【问题描述】:

我目前正在获取这样的标签列表:

<?php the_tags('<ul><li>','</li><li>','</li></ul>'); ?>

按字母顺序显示。不过我需要更改顺序。我只有 4 个可能的标签,而且我知道它们应该进入的顺序,但它不是按字母顺序排列的。

编辑:我发布了错误的代码。这就是标签的显示方式:

<?php wp_tag_cloud('smallest=9&largest=9&format=flat' );?>

我需要按自定义顺序“a”、“b”、“c”、“d”显示它

【问题讨论】:

    标签: wordpress tags


    【解决方案1】:

    我相信你应该可以先这样获取标签,然后对它们做一些事情:

    $posttags = get_the_tags();
    if ($posttags) {
      $arr = array();
      $possible_tags = array('a', 'b', 'c', 'd');
      foreach ($possible_tags as $possible) {
        if (array_search($possible, $posttags) !== FALSE) {
          array_push($arr, $possible);
        }
      }
      echo '<ul><li>'. implode('</li><li>', $arr) . '</li></ul>';
    }
    

    文档http://codex.wordpress.org/Function_Reference/get_the_tags

    【讨论】:

    • 反响很好。这看起来应该适用于个别帖子。我实际上需要订购标签云(请参阅原始问题中的编辑)。对不起。
    • $posttags = wp_tag_cloud('smallest=9&amp;largest=9&amp;format=array' ); 怎么样(其余代码相同)?
    • hmm... 最终打印出一个空的无序列表。
    • 您的标签数量是否等于您在示例中使用最小和最大指定的 9?你能在 $posttags 上做一个 var_dump 吗?
    • 最小和最大是指文本大小,不计算在内。我有 4 个标签
    【解决方案2】:

    这需要编辑the_tags 函数。我认为他们目前是按他们的 ID 排序的。您可以重新调整 ID(删除、重新添加)以更改顺序。这样可以避免额外的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2019-04-03
      • 2012-12-12
      • 2020-12-16
      相关资源
      最近更新 更多