wp_unique_prefixed_id

函式
wp_unique_prefixed_id ( $prefix = '' )
引數
  • (string) $prefix Optional. Prefix for the returned ID. Default empty string.
    Required:
    Default: (empty)
返回值
  • (string) Incremental ID per prefix.
定義位置
相關方法
wp_unique_idwp_unique_filenamewp_unique_term_slugwp_unique_post_slugwp_enqueue_media
引入
6.4.0
棄用
-

為每個不同的字首生成獨立的增量 ID。

它類似於 `wp_unique_id`,但每個字首都有自己的內部 ID 計數器,使每個字首相互獨立。ID 從 1 開始,每次呼叫都會遞增。返回值並非普遍唯一,但在整個 PHP 程序中都是唯一的,而且每個字首都很穩定。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_unique_prefixed_id( $prefix = '' ) {
static $id_counters = array();
if ( ! is_string( $prefix ) ) {
wp_trigger_error(
__FUNCTION__,
sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) )
);
$prefix = '';
}
if ( ! isset( $id_counters[ $prefix ] ) ) {
$id_counters[ $prefix ] = 0;
}
$id = ++$id_counters[ $prefix ];
return $prefix . (string) $id;
}
function wp_unique_prefixed_id( $prefix = '' ) { static $id_counters = array(); if ( ! is_string( $prefix ) ) { wp_trigger_error( __FUNCTION__, sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) ) ); $prefix = ''; } if ( ! isset( $id_counters[ $prefix ] ) ) { $id_counters[ $prefix ] = 0; } $id = ++$id_counters[ $prefix ]; return $prefix . (string) $id; }
function wp_unique_prefixed_id( $prefix = '' ) {
	static $id_counters = array();

	if ( ! is_string( $prefix ) ) {
		wp_trigger_error(
			__FUNCTION__,
			sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) )
		);
		$prefix = '';
	}

	if ( ! isset( $id_counters[ $prefix ] ) ) {
		$id_counters[ $prefix ] = 0;
	}

	$id = ++$id_counters[ $prefix ];

	return $prefix . (string) $id;
}

常見問題

FAQs
檢視更多 >