Created
November 12, 2023 23:22
-
-
Save kadimi/4543f0246ba1bea03eec4125809737a4 to your computer and use it in GitHub Desktop.
SportsPress - Timeline Fix
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
<?php | |
/** | |
* Custom sorting function for timeline elements. | |
* | |
* | |
* ## The Problem | |
* | |
* When many timeline elements have the same minute(e.g, a goal and an assist | |
* both at 33'), they're thrown on the timeline in a random order are sorted. | |
* This custom sorting function fixes this problem. It sorts the elements based on | |
* the performance columns menu orders. | |
* | |
* ## How to use | |
* | |
* Override the timeline template(s) you're intereted in and add this code after | |
* the line that says `$timeline = $event->timeline( false, true );`. | |
*/ | |
usort($timeline, function ($a, $b) { | |
if ($a['time'] != $b['time']) { | |
return $a['time'] > $b['time']; | |
} | |
static $performance = []; | |
$a_key = $a['key']; | |
$b_key = $b['key']; | |
if (empty($performance[$a_key])) { | |
$performance[$a_key] = get_posts([ | |
'post_type' => 'sp_performance', | |
'name' => $a_key, | |
])[0]; | |
} | |
if (empty($performance[$b_key])) { | |
$performance[$b_key] = get_posts([ | |
'post_type' => 'sp_performance', | |
'name' => $b_key, | |
])[0]; | |
} | |
return $performance[$a_key]->menu_order < $performance[$b_key]->menu_order; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment