get_attached_file()

get_attached_file( int $attachment_id, bool $unfiltered…

get_attached_file( int $attachment_id, bool $unfiltered = false )

基于附件ID检索附加文件路径。
Retrieve attached file path based on attachment ID.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

默认情况下,路径将通过“get_attached_file”筛选器,但如果向get_attached_file()的$unfiltered参数传递true,则将返回未筛选的文件路径。

该函数通过获取名为“wp_attached_file”的单个post元名称并返回它来工作。这是一个方便的函数,用于防止查找元名称,并提供通过筛选器发送附加文件名的机制。


参数(Parameters)

参数 类型 必填 说明
$attachment_id (int) 必需 附件ID。
$unfiltered (bool) 可选 是否应用筛选器。

返回(Return)

(string|false)附加文件应位于的文件路径,否则为false。


源码(Source)

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file( $attachment_id, $unfiltered = false ) {
	$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
	// If the file is relative, prepend upload dir.
	if ( $file && 0 !== strpos($file, '/') && !preg_match('|^.:|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
		$file = $uploads['basedir'] . "/$file";
	if ( $unfiltered )
		return $file;

	/**
	 * Filter the attached file based on the given ID.
	 *
	 * @since 2.1.0
	 *
	 * @param string $file          Path to attached file.
	 * @param int    $attachment_id Attachment ID.
	 */
	return apply_filters( 'get_attached_file', $file, $attachment_id );
}
更新版本 源码位置 使用 被使用
2.0.0 wp-includes/post.php:498 24 4

笔记(Notes)

实例

类别:WordPress 函数手册

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册