源代碼
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()
是一個(gè)內(nèi)部函數(shù),用于對(duì)時(shí)區(qū)選擇進(jìn)行排序。這個(gè)函數(shù)不是公開文檔化的,通常用于WordPress后臺(tái)設(shè)置中,特別是當(dāng)用戶需要選擇他們的時(shí)區(qū)時(shí)。
這個(gè)函數(shù)是作為回調(diào)函數(shù)傳遞給 usort()
函數(shù),用于比較兩個(gè)時(shí)區(qū)名稱,并按字母順序?qū)λ鼈冞M(jìn)行排序。
以下是 _wp_timezone_choice_usort_callback()
函數(shù)的基本用法:
usort( $timezones, '_wp_timezone_choice_usort_callback' );
參數(shù)解釋如下:
$timezones
:一個(gè)包含時(shí)區(qū)名稱的數(shù)組。
使用_wp_timezone_choice_usort_callback()
的步驟:
- 獲取時(shí)區(qū)數(shù)組:首先,你需要獲取一個(gè)包含時(shí)區(qū)名稱的數(shù)組。
- 使用
usort()
和回調(diào)函數(shù)進(jìn)行排序:然后,使用usort()
函數(shù)和_wp_timezone_choice_usort_callback()
作為回調(diào)來對(duì)時(shí)區(qū)數(shù)組進(jìn)行排序。
下面是一個(gè)使用_wp_timezone_choice_usort_callback()
函數(shù)的例子:
<?php
// 獲取所有時(shí)區(qū)
$all_timezones = timezone_identifiers_list();
// 過濾掉一些不需要的時(shí)區(qū)(可選)
$timezones = array_filter( $all_timezones, function( $tz ) {
return strpos( $tz, '/' ) !== false;
});
// 使用 _wp_timezone_choice_usort_callback() 對(duì)時(shí)區(qū)進(jìn)行排序
usort( $timezones, '_wp_timezone_choice_usort_callback' );
// 輸出排序后的時(shí)區(qū)列表
foreach ( $timezones as $timezone ) {
echo $timezone . '<br>';
}
?>
在這個(gè)例子中,我們首先獲取了所有時(shí)區(qū)標(biāo)識(shí)符,然后過濾掉了一些不需要的時(shí)區(qū)(這是一個(gè)可選步驟)。接著,我們使用 usort()
函數(shù)和 _wp_timezone_choice_usort_callback()
回調(diào)函數(shù)對(duì)時(shí)區(qū)數(shù)組進(jìn)行了排序,并輸出了排序后的時(shí)區(qū)列表。
需要注意的是,由于 _wp_timezone_choice_usort_callback()
是一個(gè)內(nèi)部函數(shù),它的使用并不推薦在公開的主題或插件代碼中。這個(gè)函數(shù)可能在WordPress的未來版本中發(fā)生變化或被移除。如果你需要對(duì)時(shí)區(qū)進(jìn)行排序,建議直接使用PHP內(nèi)置的排序函數(shù),如 sort()
或 asort()
,并結(jié)合適當(dāng)?shù)谋容^函數(shù)來達(dá)到相同的效果。
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » WordPress函數(shù)_wp_timezone_choice_usort_callback()用法