修复友链分类未设置 term_priority 时不显示的问题#1393
Open
wanyulaowang wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 修复友情链接模板中“链接分类未设置 term_priority 时被过滤导致不显示/不分组”的问题,通过改为获取全部 link_category 分类并在 PHP 层进行排序,确保未设置优先级的分类也能正常展示。
Changes:
- 移除
get_terms()中对meta_key => term_priority的依赖,避免未设置元数据的分类被过滤。 - 新增
usort()自定义排序:有term_priority的按数值降序;未设置的排在后面,且未设置时按名称排序。
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+918
to
+930
| // 按 term_priority 排序,未设置的排最后 | ||
| usort($linkcats, function($a, $b) { | ||
| $pa = get_term_meta($a->term_id, 'term_priority', true); | ||
| $pb = get_term_meta($b->term_id, 'term_priority', true); | ||
|
|
||
| // 都没设置,按名称排序 | ||
| if ($pa === '' && $pb === '') { | ||
| return strcmp($a->name, $b->name); | ||
| } | ||
| // 没设置的放最后 | ||
| if ($pa === '') return 1; | ||
| if ($pb === '') return -1; | ||
|
|
Comment on lines
+925
to
+932
| return strcmp($a->name, $b->name); | ||
| } | ||
| // 没设置的放最后 | ||
| if ($pa === '') return 1; | ||
| if ($pb === '') return -1; | ||
|
|
||
| // 降序:数字大的在前 | ||
| return intval($pb) - intval($pa); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
当前
get_link_items()函数在获取链接分类时,使用了meta_key => 'term_priority'参数:这导致 所有未设置 term_priority 元数据的链接分类都会被过滤掉,其下的友链无法按分类分组显示,而是全部混在一起平铺输出(或完全不显示)。
复现步骤
进入后台「链接 → 链接分类」,新建一个分类(不设置优先级)
添加若干链接并分配到该分类
创建页面并选择「友情链接模板」
前台查看友链页面,发现该分类下的链接没有按组显示
预期行为
即使没有设置 term_priority,链接分类也应该正常显示,并按名称或其他合理顺序排列。
实际行为
未设置 term_priority 的分类被 get_terms() 过滤,分类下的链接无法分组展示。
解决方案
临时解决方案
在数据库中执行sql语句,将缺失的分类映射添加回来
执行以下 SQL,为对应分类ID(在wp_terms表中找到term_id;或者可以在后台链接分类列表的 URL 中看到 tag_ID=xxx)添加优先级(term_priority)(数字越大越靠前):
代码解决方案
移除 get_terms() 中的 meta_key 限制,改为获取所有 link_category 分类,然后在 PHP 层面通过 usort() 实现优先级排序:
有 term_priority 的分类:继续按数字大小降序排列(数字越大越靠前),保持原有行为
没有 term_priority 的分类:也能正常显示,默认排在有优先级的分类之后;若都没有设置,则按分类名称升序排列
向后兼容性
✅ 完全兼容:已有 term_priority 的分类排序逻辑不变,只是额外支持了未设置优先级的分类。
AI声明:此问题原因排查、解决方案均有AI参与