Background: I'm working on a gaming blog, and I've created a custom taxonomy for Games.
I've added a Game List page which will (unsurprisingly) list all the non-empty terms within that taxonomy. Knowing that this list could get rather large in the future, I want to sort and group terms by their first character with a quick access bar at the top of the page and a "back to top" link at the end of each section.
I've come up with the following function to retrieve the sorted list, and it works perfectly, but I'm wondering if there is a more efficient way to pull this off (like some core option I haven't discovered yet).
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;
}
}