源代碼
File: wp-includes/functions.php
function _wp_timezone_choice_usort_callback( $a, $b ) {
// Don't use translated versions of Etc
if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
// Make the order of these more like the old dropdown
if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
}
if ( 'UTC' === $a['city'] ) {
if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
return 1;
}
return -1;
}
if ( 'UTC' === $b['city'] ) {
if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
return -1;
}
return 1;
}
return strnatcasecmp( $a['city'], $b['city'] );
}
if ( $a['t_continent'] == $b['t_continent'] ) {
if ( $a['t_city'] == $b['t_city'] ) {
return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
}
return strnatcasecmp( $a['t_city'], $b['t_city'] );
} else {
// Force Etc to the bottom of the list
if ( 'Etc' === $a['continent'] ) {
return 1;
}
if ( 'Etc' === $b['continent'] ) {
return -1;
}
return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
}
}
更新日志
Version | 描述 |
---|---|
2.9.0 | Introduced. |
在WordPress中,_wp_timezone_choice_usort_callback()
是一個內(nèi)部函數(shù),用于對時區(qū)選擇進行排序。這個函數(shù)不是公開文檔化的,通常用于WordPress后臺設置中,特別是當用戶需要選擇他們的時區(qū)時。
這個函數(shù)是作為回調函數(shù)傳遞給 usort()
函數(shù),用于比較兩個時區(qū)名稱,并按字母順序對它們進行排序。
以下是 _wp_timezone_choice_usort_callback()
函數(shù)的基本用法:
usort( $timezones, '_wp_timezone_choice_usort_callback' );
參數(shù)解釋如下:
$timezones
:一個包含時區(qū)名稱的數(shù)組。
使用_wp_timezone_choice_usort_callback()
的步驟:
- 獲取時區(qū)數(shù)組:首先,你需要獲取一個包含時區(qū)名稱的數(shù)組。
- 使用
usort()
和回調函數(shù)進行排序:然后,使用usort()
函數(shù)和_wp_timezone_choice_usort_callback()
作為回調來對時區(qū)數(shù)組進行排序。
下面是一個使用_wp_timezone_choice_usort_callback()
函數(shù)的例子:
<?php
// 獲取所有時區(qū)
$all_timezones = timezone_identifiers_list();
// 過濾掉一些不需要的時區(qū)(可選)
$timezones = array_filter( $all_timezones, function( $tz ) {
return strpos( $tz, '/' ) !== false;
});
// 使用 _wp_timezone_choice_usort_callback() 對時區(qū)進行排序
usort( $timezones, '_wp_timezone_choice_usort_callback' );
// 輸出排序后的時區(qū)列表
foreach ( $timezones as $timezone ) {
echo $timezone . '<br>';
}
?>
在這個例子中,我們首先獲取了所有時區(qū)標識符,然后過濾掉了一些不需要的時區(qū)(這是一個可選步驟)。接著,我們使用 usort()
函數(shù)和 _wp_timezone_choice_usort_callback()
回調函數(shù)對時區(qū)數(shù)組進行了排序,并輸出了排序后的時區(qū)列表。
需要注意的是,由于 _wp_timezone_choice_usort_callback()
是一個內(nèi)部函數(shù),它的使用并不推薦在公開的主題或插件代碼中。這個函數(shù)可能在WordPress的未來版本中發(fā)生變化或被移除。如果你需要對時區(qū)進行排序,建議直接使用PHP內(nèi)置的排序函數(shù),如 sort()
或 asort()
,并結合適當?shù)谋容^函數(shù)來達到相同的效果。
未經(jīng)允許不得轉載:445IT之家 » WordPress函數(shù)_wp_timezone_choice_usort_callback()用法