函数文档

wp_attachment_is_image()

💡 云策文档标注

概述

wp_attachment_is_image() 函数用于判断一个附件是否为图像。它是 wp_attachment_is() 的包装函数,简化了图像类型检查。

关键要点

  • 函数接受一个可选参数 $post,可以是附件 ID 或 WP_Post 对象,默认为全局 $post
  • 返回布尔值,表示附件是否为图像
  • 自 WordPress 4.2.0 起,改为 wp_attachment_is() 的包装函数,并支持传递 WP_Post 对象
  • 在主题开发中常用于条件判断,例如检查附件类型以进行相应处理

代码示例

$id = 37;
if ( wp_attachment_is_image( $id ) ) {
    printf( 'Post %d is an image!', $id );
} else {
    printf( 'Post %d is not an image.', $id );
}

📄 原文内容

Determines whether an attachment is an image.

Description

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Parameters

$postint|WP_Postoptional
Attachment ID or object. Default is global $post.

Default:null

Return

bool Whether the attachment is an image.

Source

function wp_attachment_is_image( $post = null ) {
	return wp_attachment_is( 'image', $post );
}

Changelog

Version Description
4.2.0 Modified into wrapper for wp_attachment_is() and allowed WP_Post object to be passed.
2.1.0 Introduced.

User Contributed Notes