WordPress函数文档get_attached_file()
获取附件(图片/文件)的文件路径 描述 译文 按附件编号检索附件文件的路径。 也可以选择性地通过’get_at…
获取附件(图片/文件)的文件路径
描述
译文
按附件编号检索附件文件的路径。
也可以选择性地通过’get_attached_file’过滤器发送路径,但默认情况下只返回未过滤的文件路径。
函数获取单个文章的名为”_wp_attached_file”的meta并将其返回。该函数可以防止查找meta名称,也为通过过滤器发送附件文件提供了方法。
原文
Retrieve attached file path based on attachment ID.
You can optionally send it through the ‘get_attached_file’ filter, but by default it will just 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 file name through a filter.
用法
<?php get_attached_file( $attachment_id, $unfiltered ); ?>
参数
$attachment_id
(integer) (必填) Attachment ID.
默认值: None
$unfiltered
(boolean) (可选) Whether to apply filters or not.
默认值: false
返回值
(string)
The file path to the attached file.
示例
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
$fullsize_path = get_attached_file( $attachment_id ); // Full path
|
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
$filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name
|
注意
- 使用到: apply_filters() to call get_attached_file() on file path and $attachment_id.
- 使用到: get_post_meta() on $attachment_id, the ‘_wp_attached_file‘ meta name.
历史
添加于 版本: 2.0.0
源文件
get_attached_file() 函数的代码位于 wp-includes/post.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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 );
}
|
相关
Attachment Function(函数)s:
get_children(),
get attached media(),
the_attachment_link(),
get_attachment_link(),
wp_get_attachment_link(),
wp_get_attachment_image(),
wp_get_attachment_image_src(),
wp_get_attachment_url(),
wp_get_attachment_thumb_file(),
wp_get_attachment_thumb_url(),
is_attachment(),
wp_get_attachment_metadata()
- 原文:http://codex.wordpress.org/Function_Reference/get_attached_file
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!