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
查看更多 >