验证附件是否为给定类型。
原型
wp_attachment_is( string $type, int|WP_Post $post = null )
参数
$type
(string)
(Required)
附件类型。接受“图像”,“音频”或“视频”。
$post
(int|WP_Post)
(Optional)
附件ID或对象。默认为全球$ post。
返回值
(bool)
如果是其中一个接受的类型,则为True,否则为false。
源文件
路径:wp-includes/post.php
<?php
...
function wp_attachment_is( $type, $post = null ) {
if ( ! $post = get_post( $post ) ) {
return false;
}
if ( ! $file = get_attached_file( $post->ID ) ) {
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' );
return in_array( $ext, $image_exts );
case 'audio':
return in_array( $ext, wp_get_audio_extensions() );
case 'video':
return in_array( $ext, wp_get_video_extensions() );
default:
return $type === $ext;
}
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_attachment_is/