wp_attachment_is

函式
wp_attachment_is ( $type, $post = null )
引數
  • (string) $type Attachment type. Accepts 'image', 'audio', or 'video'.
    Required:
  • (int|WP_Post) $post Optional. Attachment ID or object. Default is global $post.
    Required:
    Default: null
返回值
  • (bool) True if one of the accepted types, false otherwise.
定義位置
相關方法
wp_attachment_is_imagewp_get_attachment_linkthe_attachment_linkswp_count_attachmentswp_get_attachment_image
引入
4.2.0
棄用
-

wp_attachment_is: 這是一個實用函式,用於確定一個文章的ID是否代表一個附件。它接受一個文章的ID作為引數,如果該文章是一個附件,則返回true,如果不是,則返回false: 這個函式根據WordPress中已知的附件型別列表檢查指定的文章ID的文章型別,其中包括”附件”、”修訂”和”導航選單_專案”。如果在這個列表中找到了文章型別,那麼這個函式就返回true。

驗證一個附件是一個特定的型別。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_attachment_is( $type, $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$file = get_attached_file( $post->ID );
if ( ! $file ) {
return false;
}
if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
return true;
}
$check = wp_check_filetype( $file );
if ( empty( $check['ext'] ) ) {
return false;
}
$ext = $check['ext'];
if ( 'import' !== $post->post_mime_type ) {
return $type === $ext;
}
switch ( $type ) {
case 'image':
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
return in_array( $ext, $image_exts, true );
case 'audio':
return in_array( $ext, wp_get_audio_extensions(), true );
case 'video':
return in_array( $ext, wp_get_video_extensions(), true );
default:
return $type === $ext;
}
}
function wp_attachment_is( $type, $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } $file = get_attached_file( $post->ID ); if ( ! $file ) { return false; } if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { return true; } $check = wp_check_filetype( $file ); if ( empty( $check['ext'] ) ) { return false; } $ext = $check['ext']; if ( 'import' !== $post->post_mime_type ) { return $type === $ext; } switch ( $type ) { case 'image': $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ); return in_array( $ext, $image_exts, true ); case 'audio': return in_array( $ext, wp_get_audio_extensions(), true ); case 'video': return in_array( $ext, wp_get_video_extensions(), true ); default: return $type === $ext; } }
function wp_attachment_is( $type, $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$file = get_attached_file( $post->ID );

	if ( ! $file ) {
		return false;
	}

	if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
		return true;
	}

	$check = wp_check_filetype( $file );

	if ( empty( $check['ext'] ) ) {
		return false;
	}

	$ext = $check['ext'];

	if ( 'import' !== $post->post_mime_type ) {
		return $type === $ext;
	}

	switch ( $type ) {
		case 'image':
			$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
			return in_array( $ext, $image_exts, true );

		case 'audio':
			return in_array( $ext, wp_get_audio_extensions(), true );

		case 'video':
			return in_array( $ext, wp_get_video_extensions(), true );

		default:
			return $type === $ext;
	}
}

常見問題

FAQs
檢視更多 >