返回介绍

wp_get_attachment_image_src()

发布于 2017-09-11 11:59:10 字数 8152 浏览 1068 评论 0 收藏 0

wp_get_attachment_image_src( int $attachment_id,  string|array $size = 'thumbnail',  bool $icon = false )

Retrieve an image to represent an attachment.


description

A mime icon for files, thumbnail or intermediate size for images.

The returned array contains four values: the URL of the attachment image src, the width of the image file, the height of the image file, and a boolean representing whether the returned array describes an intermediate (generated) image size or the original, full-sized upload.


参数

$attachment_id

(int) (Required) Image attachment ID.

$size

(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'thumbnail'

$icon

(bool) (Optional) Whether the image should be treated as an icon.

Default value: false


返回值

(false|array) Returns an array (url, width, height, is_intermediate), or false, if no image is available.


源代码

File: wp-includes/media.php

function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
	// get a thumbnail or intermediate image if there is one
	$image = image_downsize( $attachment_id, $size );
	if ( ! $image ) {
		$src = false;

		if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) {
			/** This filter is documented in wp-includes/post.php */
			$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );

			$src_file = $icon_dir . '/' . wp_basename( $src );
			@list( $width, $height ) = getimagesize( $src_file );
		}

		if ( $src && $width && $height ) {
			$image = array( $src, $width, $height );
		}
	}
	/**
	 * Filters the image src result.
	 *
	 * @since 4.3.0
	 *
	 * @param array|false  $image         Either array with src, width & height, icon src, or false.
	 * @param int          $attachment_id Image attachment ID.
	 * @param string|array $size          Size of image. Image size or array of width and height values
	 *                                    (in that order). Default 'thumbnail'.
	 * @param bool         $icon          Whether the image should be treated as an icon. Default false.
	 */
	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/media.php: wp_get_attachment_image_src
  • wp-includes/formatting.php: wp_basename()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: image_downsize()
  • wp-includes/post.php: wp_mime_type_icon()
  • wp-includes/post.php: icon_dir
  • Show 1 more use Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::prepare_item_for_response()
  • wp-includes/embed.php: get_oembed_response_data_rich()
  • wp-includes/media.php: wp_get_attachment_image_srcset()
  • wp-includes/media.php: wp_get_attachment_image_sizes()
  • wp-includes/media.php: wp_get_attachment_image_url()
  • wp-admin/includes/image-edit.php: wp_save_image()
  • wp-admin/includes/media.php: edit_form_image_editor()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2()
  • wp-admin/custom-background.php: Custom_Background::wp_set_background_image()
  • wp-admin/custom-background.php: Custom_Background::handle_upload()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/media.php: wp_playlist_shortcode()
  • wp-includes/media.php: wp_get_attachment_image()
  • Show 9 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: 3You must log in to vote on the helpfulness of this note Contributed by Codex

    Default Usage

    
    $image_attributes = wp_get_attachment_image_src( $attachment_id = 8 );
    if ( $image_attributes ) : ?>
    	<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
    <?php endif; ?>
    
  2. Show the first image associated with the post
    This function retrieves the first image associated with a post.

    
    /**
     * Output a post's first image.
     *
     * @param int $post_id Post ID.
     */
    function wpdocs_echo_first_image( $post_id ) {
    	$args = array(
    		'posts_per_page' => 1,
    		'order'          => 'ASC',
    		'post_mime_type' => 'image',
    		'post_parent'    => $post_id,
    		'post_status'    => null,
    		'post_type'      => 'attachment',
    	);
    
    	$attachments = get_children( $args );
    
    	if ( $attachments ) {
    		echo '<img src="' . wp_get_attachment_thumb_url( $attachments[0]->ID ) . '" class="current">';
    	}
    }
    

    Tips: Return values ( [1] width [2] height [3] resize ) seems to be empty when using the Jetpack plugin.

    Change Icon Directory
    WordPress can use media icons to represent attachment files on your blog and in the Admin interface, if those icons are available.
    For images, it returns the thumbnail. For other media types, it looks for image files named by media type (e.g., audio.jpg) in the directory wp-includes/images/crystal/.

    This example shows how you can change this directory to a folder called “images” in your theme: wp-content/themes/yourtheme/images. Create the folder and put “media type images” in there. To tell WordPress the directory has changed, put this in the current theme’s functions.php file:

    
    add_filter( 'icon_dir', 'wpdocs_theme_icon_directory' );
    add_filter( 'icon_dir_uri', 'wpdocs_theme_icon_uri' );
    
    /*
     * Return my desired icon directory
     */
    function wpdocs_theme_icon_directory( $icon_dir ) {
    	return get_stylesheet_directory() . '/images';
    }
    
    /*
     * Return my desired icon URI
     */
    function wpdocs_theme_icon_uri( $icon_dir ) {
    	return get_stylesheet_directory_uri() . '/images'; 
    }
    

    Retrieve the post thumbnail url sized as 220 if thumbnail exists.

    
    $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, 'numberposts' => 5 );
    
    $posts = get_posts( $args );
    
    foreach($posts as $post) {
    	$thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array('220','220'), true );
    	$thumbnail_url = $thumbnail_url[0];
    	echo ( !empty($thumbnail_url) ) ? $thumbnail_url : 'No thumb!';
    }
    

    Theis_intermediate

    
    array{
    	[0] => url,
    	[1] => width</em>
    	[2] => height</em>
    	[4] => is_intermediate
    }
    

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

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

发布评论

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