返回介绍

wp_get_attachment_url()

发布于 2017-09-11 12:00:28 字数 6963 浏览 1154 评论 0 收藏 0

wp_get_attachment_url( int $post_id )

Retrieve the URL for an attachment.


description


参数

$post_id

(int) (Optional) Attachment ID. Default 0.


返回值

(string|false) Attachment URL, otherwise false.


源代码

File: wp-includes/post.php

function wp_get_attachment_url( $post_id = 0 ) {
	$post_id = (int) $post_id;
	if ( !$post = get_post( $post_id ) )
		return false;

	if ( 'attachment' != $post->post_type )
		return false;

	$url = '';
	// Get attached file.
	if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) {
		// Get upload directory.
		if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) {
			// Check that the upload base exists in the file location.
			if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
				// Replace file location with url location.
				$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
			} elseif ( false !== strpos($file, 'wp-content/uploads') ) {
				// Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
				$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file );
			} else {
				// It's a newly-uploaded file, therefore $file is relative to the basedir.
				$url = $uploads['baseurl'] . "/$file";
			}
		}
	}

	/*
	 * If any of the above options failed, Fallback on the GUID as used pre-2.7,
	 * not recommended to rely upon this.
	 */
	if ( empty($url) ) {
		$url = get_the_guid( $post->ID );
	}

	// On SSL front end, URLs should be HTTPS.
	if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) {
		$url = set_url_scheme( $url );
	}

	/**
	 * Filters the attachment URL.
	 *
	 * @since 2.1.0
	 *
	 * @param string $url     URL for the given attachment.
	 * @param int    $post_id Attachment ID.
	 */
	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );

	if ( empty( $url ) )
		return false;

	return $url;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_get_upload_dir()
  • wp-includes/media.php: _wp_get_attachment_relative_path()
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/load.php: is_admin()
  • wp-includes/load.php: is_ssl()
  • wp-includes/link-template.php: set_url_scheme()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: get_the_guid()
  • wp-includes/post.php: wp_get_attachment_url
  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: get_post()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/widgets/class-wp-widget-media-audio.php: WP_Widget_Media_Audio::render_media()
  • wp-includes/widgets/class-wp-widget-media-video.php: WP_Widget_Media_Video::render_media()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::import_theme_starter_content()
  • wp-includes/theme.php: get_header_video_url()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::prepare_item_for_response()
  • wp-admin/includes/class-wp-site-icon.php: WP_Site_Icon::create_attachment_object()
  • wp-admin/includes/ajax-actions.php: wp_ajax_crop_image()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/image-edit.php: wp_save_image()
  • wp-admin/includes/image.php: _load_image_to_edit_path()
  • wp-admin/includes/template.php: _media_states()
  • wp-admin/includes/media.php: edit_form_image_editor()
  • wp-admin/includes/media.php: attachment_submitbox_metadata()
  • wp-admin/includes/media.php: get_attachment_fields_to_edit()
  • wp-admin/includes/media.php: media_sideload_image()
  • wp-admin/includes/media.php: image_link_input_fields()
  • wp-admin/custom-header.php: Custom_Image_Header::create_attachment_object()
  • wp-admin/custom-header.php: Custom_Image_Header::ajax_header_crop()
  • wp-admin/custom-header.php: Custom_Image_Header::step_3()
  • wp-includes/theme.php: _delete_attachment_theme_mod()
  • wp-includes/theme.php: get_uploaded_header_images()
  • wp-includes/deprecated.php: get_the_attachment_link()
  • wp-includes/deprecated.php: get_attachment_icon_src()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/post-template.php: prepend_attachment()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/media.php: wp_video_shortcode()
  • wp-includes/media.php: wp_playlist_shortcode()
  • wp-includes/media.php: wp_audio_shortcode()
  • wp-includes/media.php: image_get_intermediate_size()
  • wp-includes/media.php: image_downsize()
  • wp-includes/post.php: wp_get_attachment_thumb_url()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_media_item()
  • Show 29 more used by Hide more used by

User Contributed Notes

  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

    Basic Example

    
    <?php echo wp_get_attachment_url( 12 ); ?> 
    

    Outputs something like http://example.net/wp-content/uploads/filename

  2. Use Post Thumbnail as Background Image

    
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
        if ( has_post_thumbnail() ) {
            $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
            echo '<div style="background-image:url('.$feat_image_url.');"></div>';
        }
        endwhile;
    endif;
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文