10/25/2012 - 仍未解决!请看下面:
我的客户端有一个WordPress标签云(标签数组),标签包含["]字符以及某些标签的[The]前缀.即:
"rose"
"autumn"
The Abby
The Cloud
The Elephant
Run Code Online (Sandbox Code Playgroud)
显然,引号[[]]中包含的所有标签都在列表顶部排序,所有以[The]前缀开头的单词都在字母[T]附近排序(遵循逻辑ASC顺序).
在我身上撒了一个:"所有标签(在WP标签云中)必须按顺序排序,但包含[""]或[The]字符的那些必须按时间顺序与所有其他标签一起排序,忽略["]和[The]前缀.
我研究了WP核心功能:
**function wp_generate_tag_cloud**
Run Code Online (Sandbox Code Playgroud)
但我不知道从哪里开始.在原始SQL语句中,我可以使用trim()从标签云数组中过滤出[""]和[]字符,但这只是一个我不知道如何应用的思想.
wp_generate_tag_cloud()
调用名为 的过滤器tag_cloud_sort
,该过滤器可以覆盖参数中指定的排序顺序$args
。过滤tag_cloud_sort
器接收一组标签和$args
传递给 的实际参数wp_generate_tag_cloud()
,因此它可以检查调用的完整设置wp_generate_tag_cloud()
并相应地调整其行为。
你可以尝试这样的事情:
function custom_tag_sort($tags, $args) {
if ($args['orderby'] != 'name') {
// do not reorder if sort order is not by name.
// wp_generate_tag_cloud() is smart enough to notice order
// is not changed and will proceed with its regular sort logic.
return $tags;
}
uasort($tags, 'custom_tag_sort_compare');
}
function custom_tag_sort_compare($a, $b) {
return strnatcasecmp(
custom_tag_sort_normalize($a->name),
custom_tag_sort_normalize($b->name)
);
}
function custom_tag_sort_normalize($tag) {
// strip quote marks
$tag = trim($tag, '"');
// strip leading definitive article
$tag = preg_replace('/^\s*the\s+/i', '', $tag);
return $tag;
}
add_filter('tag_cloud_sort', 'custom_tag_sort');
Run Code Online (Sandbox Code Playgroud)
免责声明:我是在粗略地检查了该wp_generate_tag_cloud()
函数之后才写下这篇文章的。我还没有在实时 WordPress 安装上测试过它;我仅验证了排序功能在您的示例标签云上正常工作:
The Abby
"autumn"
The Cloud
The Elephant
"rose"
Run Code Online (Sandbox Code Playgroud)