如何在 WordPress 中按字母顺序对标签云进行排序
How to sort tag clouds alphabetically in WordPress
我正在通过 WordPress 管理员添加一个新的 “区域”标签,它应该在最后,因为 字母 Z 是字母表中的最后一个。但是我的标签“Zone”显示在标签云中列表最开头的页面上。
我的标签显示代码:
<article class="Tags_Main_Block">
<p class="tag-titles">Tags Cloud:</p>
<div class="opase">
<?php
wp_tag_cloud( [
'smallest' => 14,
'largest' => 14,
'unit' => 'pt',
'number' => 0,
'format' => 'flat',
'separator' => "",
'orderby' => 'name',
'order' => 'ASC',
'exclude' => null,
'include' => null,
'link' => 'view',
'taxonomy' => 'post_tag',
'echo' => true,
'topic_count_text_callback' => 'default_topic_count_text',
] );
?>
</div>
</article>
首先我们使用 wp_tag_cloud()
获取与 post 关联的标签。然后,传入一些参数。首先,'format'
参数将标签放入一个数组中,这样我们就可以循环遍历它们。然后,我们将它设置为不回显,因为我们想将它保存到一个变量中。然后您需要使用 asort()
函数。这是 PHP 中的一个函数,因此它不是 WordPress 独有的。
这里有更多可以传递给函数的参数:https://developer.wordpress.org/reference/functions/wp_generate_tag_cloud/
$tagsExample = wp_tag_cloud(
['format' => 'array',
'echo' => false
]);
asort($tagsExample);
foreach($tagsExample as $tag) {
echo $tag;
}
我正在通过 WordPress 管理员添加一个新的 “区域”标签,它应该在最后,因为 字母 Z 是字母表中的最后一个。但是我的标签“Zone”显示在标签云中列表最开头的页面上。
我的标签显示代码:
<article class="Tags_Main_Block">
<p class="tag-titles">Tags Cloud:</p>
<div class="opase">
<?php
wp_tag_cloud( [
'smallest' => 14,
'largest' => 14,
'unit' => 'pt',
'number' => 0,
'format' => 'flat',
'separator' => "",
'orderby' => 'name',
'order' => 'ASC',
'exclude' => null,
'include' => null,
'link' => 'view',
'taxonomy' => 'post_tag',
'echo' => true,
'topic_count_text_callback' => 'default_topic_count_text',
] );
?>
</div>
</article>
首先我们使用 wp_tag_cloud()
获取与 post 关联的标签。然后,传入一些参数。首先,'format'
参数将标签放入一个数组中,这样我们就可以循环遍历它们。然后,我们将它设置为不回显,因为我们想将它保存到一个变量中。然后您需要使用 asort()
函数。这是 PHP 中的一个函数,因此它不是 WordPress 独有的。
这里有更多可以传递给函数的参数:https://developer.wordpress.org/reference/functions/wp_generate_tag_cloud/
$tagsExample = wp_tag_cloud(
['format' => 'array',
'echo' => false
]);
asort($tagsExample);
foreach($tagsExample as $tag) {
echo $tag;
}