背景:我在一个游戏博客上工作,我为游戏创建了一个自定义分类法。
我添加了一个游戏列表页面,它将(毫不奇怪地)列出所有non-empty 该分类法中的术语。知道这个列表将来可能会变得相当大,我想通过页面顶部的快速访问栏和每个部分末尾的“返回顶部”链接,按照术语的第一个字符对其进行排序和分组。
我想出了以下函数来检索排序列表,它工作得很好,但我想知道是否有更有效的方法来实现这一点(比如我还没有发现的一些核心选项)。
function bmg_getGameList() {
$games = get_terms(BMG_GAME_SLUG, true);
if ($games && !is_wp_error($games)) {
$gameList = array(
"#" => array(), "A" => array(), "B" => array(),
"C" => array(), "D" => array(), "E" => array(),
"F" => array(), "G" => array(), "H" => array(),
"I" => array(), "J" => array(), "K" => array(),
"L" => array(), "M" => array(), "N" => array(),
"O" => array(), "P" => array(), "Q" => array(),
"R" => array(), "S" => array(), "T" => array(),
"U" => array(), "V" => array(), "W" => array(),
"X" => array(), "Y" => array(), "Z" => array(),
);
foreach($games as $game) {
$normalizedTitle = strtoupper($game->name);
$normalizedTitle = preg_replace("/^THE ",\'\', $normalizedTitle,1);
/* NOTE: strncmp returns 0 if the comparison is EQUAL.
We want equality in this case since we\'re
sorting games into buckets based on first
characters */
if (strncmp($normalizedTitle, "A", 1) === 0) {
$gameList["A"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "B", 1) === 0) {
$gameList["B"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "C", 1) === 0) {
$gameList["C"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "D", 1) === 0) {
$gameList["D"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "E", 1) === 0) {
$gameList["E"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "F", 1) === 0) {
$gameList["F"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "G", 1) === 0) {
$gameList["G"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "H", 1) === 0) {
$gameList["H"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "I", 1) === 0) {
$gameList["I"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "J", 1) === 0) {
$gameList["J"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "K", 1) === 0) {
$gameList["K"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "L", 1) === 0) {
$gameList["L"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "M", 1) === 0) {
$gameList["M"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "N", 1) === 0) {
$gameList["N"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "O", 1) === 0) {
$gameList["O"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "P", 1) === 0) {
$gameList["P"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "Q", 1) === 0) {
$gameList["Q"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "R", 1) === 0) {
$gameList["R"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "S", 1) === 0) {
$gameList["S"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "T", 1) === 0) {
$gameList["T"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "U", 1) === 0) {
$gameList["U"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "V", 1) === 0) {
$gameList["V"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "W", 1) === 0) {
$gameList["W"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "X", 1) === 0) {
$gameList["X"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "Y", 1) === 0) {
$gameList["Y"][$normalizedTitle] = $game;
} else if (strncmp($normalizedTitle, "Z", 1) === 0) {
$gameList["Z"][$normalizedTitle] = $game;
} else {
$gameList["#"][$normalizedTitle] = $game;
}
}
/* Sort each letter bucket by the NORMALIZED titles
(case insensitive, ignoring "THE" - The Division
sorts under D) */
foreach ($gameList as $group) {
ksort($group);
}
return $gameList;
} else {
return false;
}
}