get_attached_file

函式
get_attached_file ( $attachment_id, $unfiltered = false )
引數
  • (int) $attachment_id Attachment ID.
    Required:
  • (bool) $unfiltered Optional. Whether to apply filters. Default false.
    Required:
    Default: false
返回值
  • (string|false) The file path to where the attached file should be, false otherwise.
定義位置
相關方法
update_attached_fileget_attached_mediaget_the_titleget_attachment_linkget_attachment_icon
引入
2.0.0
棄用
-

get_attached_file: 這個函式返回給定附件ID的附件檔案的路徑。

根據附件ID檢索附件檔案的路徑。

預設情況下,路徑將通過”get_attached_file”過濾器,但給get_attached_file()的$unfiltered引數傳遞一個true將返回未過濾的檔案路徑。

該函式的工作原理是獲取單一的文章元名稱,命名為’_wp_attached_file’並返回它: 這是一個方便的函式,以防止查詢元名稱,並提供一個機制,通過過濾器傳送附件檔名。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 ) {
$uploads = wp_get_upload_dir();
if ( false === $uploads['error'] ) {
$file = path_join( $uploads['basedir'], $file );
}
}
if ( $unfiltered ) {
return $file;
}
/**
* Filters the attached file based on the given ID.
*
* @since 2.1.0
*
* @param string|false $file The file path to where the attached file should be, false otherwise.
* @param int $attachment_id Attachment ID.
*/
return apply_filters( 'get_attached_file', $file, $attachment_id );
}
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 ) { $uploads = wp_get_upload_dir(); if ( false === $uploads['error'] ) { $file = path_join( $uploads['basedir'], $file ); } } if ( $unfiltered ) { return $file; } /** * Filters the attached file based on the given ID. * * @since 2.1.0 * * @param string|false $file The file path to where the attached file should be, false otherwise. * @param int $attachment_id Attachment ID. */ return apply_filters( 'get_attached_file', $file, $attachment_id ); }
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 ) {
		$uploads = wp_get_upload_dir();

		if ( false === $uploads['error'] ) {
			$file = path_join( $uploads['basedir'], $file );
		}
	}

	if ( $unfiltered ) {
		return $file;
	}

	/**
	 * Filters the attached file based on the given ID.
	 *
	 * @since 2.1.0
	 *
	 * @param string|false $file          The file path to where the attached file should be, false otherwise.
	 * @param int          $attachment_id Attachment ID.
	 */
	return apply_filters( 'get_attached_file', $file, $attachment_id );
}

常見問題

FAQs
檢視更多 >