I'm working on a WordPress site, trying to sort post tags, which is an array of objects with various members including slug
and name
. I've created an array of tag slug strings in priority order, and trying to sort a post's array of tags according to that array's values, with the post's slug as the key. Example:
$tagPriorityMap = array(
"research" => 0,
"strategy" => 1,
"naming" => 2,
"identity" => 3,
"packaging" => 4,
"environment" => 5,
"digital" => 6,
);
function sort_tags($a, $b) {
if ($tagPriorityMap[$a->slug] == $tagPriorityMap[$b->slug]) {
return 0;
}
return ($tagPriorityMap[$a->slug] < $tagPriorityMap[$b->slug]) ? -1 : 1;
}
If I echo some code inside the sort_tags function, it shows that $a->slug
is a string like "strategy", but it also shows that $tagPriorityMap[$a->slug]
returns no value. What am I doing wrong? Thanks.
your $tagPriorityMap
must be declared as a global variable inside the function. Jut try:
function sort_tags($a, $b) {
global $tagPriorityMap;
if ($tagPriorityMap[$a->slug] == $tagPriorityMap[$b->slug]) {
return 0;
}
return ($tagPriorityMap[$a->slug] < $tagPriorityMap[$b->slug]) ? -1 : 1;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments