^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_attached_file()用法示例

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

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.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

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.


参数

$attachment_id

(int)
(Required)
Attachment ID.

$unfiltered

(bool)
(Optional)
Whether to apply filters.

Default value: false


返回值

(string|false) The file path to where the attached file should be, false otherwise.


源代码

File: wp-includes/post.php

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_get_upload_dir() ) && false === $uploads['error'] ) ) {
		$file = $uploads['basedir'] . "/$file";
	}

	if ( $unfiltered ) {
		return $file;
	}

	/**
	 * Filters 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 );
}

更新日志

Version 描述
2.0.0 Introduced.

相关函数

Uses

  • wp-includes/functions.php:
    wp_get_upload_dir()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post.php:
    get_post_meta()
  • wp-includes/post.php:
    get_attached_file

Used By

  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::_validate_header_video()
  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::import_theme_starter_content()
  • wp-admin/includes/class-wp-media-list-table.php:
    WP_Media_List_Table::column_title()
  • wp-includes/post.php:
    wp_attachment_is()
  • wp-admin/includes/class-file-upload-upgrader.php:
    File_Upload_Upgrader::__construct()
  • wp-admin/includes/image-edit.php:
    wp_restore_image()
  • wp-admin/includes/image-edit.php:
    wp_save_image()
  • wp-admin/includes/image.php:
    _copy_image_file()
  • wp-admin/includes/image.php:
    _load_image_to_edit_path()
  • wp-admin/includes/image.php:
    wp_crop_image()
  • wp-admin/includes/media.php:
    attachment_submitbox_metadata()
  • wp-admin/includes/media.php:
    get_media_item()
  • wp-admin/custom-header.php:
    Custom_Image_Header::step_2()
  • wp-admin/custom-header.php:
    Custom_Image_Header::step_3()
  • wp-includes/deprecated.php:
    wp_load_image()
  • wp-includes/deprecated.php:
    get_attachment_icon_src()
  • wp-includes/post-template.php:
    wp_get_attachment_link()
  • wp-includes/media.php:
    wp_maybe_generate_attachment_metadata()
  • wp-includes/media.php:
    wp_prepare_attachment_for_js()
  • wp-includes/media.php:
    get_attachment_taxonomies()
  • wp-includes/post.php:
    wp_mime_type_icon()
  • wp-includes/post.php:
    wp_delete_attachment()
  • wp-includes/post.php:
    wp_get_attachment_thumb_file()
  • Show 18 more used by
    Hide more used by


  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Examples

    
    $fullsize_path = get_attached_file( $attachment_id ); // Full path
    $filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name
    
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈