返回介绍

wp_get_attachment_image_url()

发布于 2017-09-11 11:59:19 字数 2608 浏览 1079 评论 0 收藏 0

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

Get the URL of an image attachment.


description


参数

$attachment_id

(int) (Required) Image attachment ID.

$size

(string|array) (Optional) Image size to retrieve. 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


返回值

(string|false) Attachment URL or false if no image is available.


源代码

File: wp-includes/media.php

function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
	return isset( $image['0'] ) ? $image['0'] : false;
}

更新日志

Versiondescription
4.4.0Introduced.

相关函数

Uses

  • wp-includes/media.php: wp_get_attachment_image_src()

Used By

  • wp-includes/post-thumbnail-template.php: get_the_post_thumbnail_url()
  • wp-includes/general-template.php: get_site_icon_url()

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 Rinku Y
    
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'home-slide-img-mobile', 640, 1072, true ); //resize, crop in functions.php
    

    Now let display somewhere:

    
    $imgid = 6; //need to get it dynamically
    $imgurldesktop = wp_get_attachment_image_url( $imgid, '' ); //use default image size
    $imgurlmobile = wp_get_attachment_image_url( $imgid, 'home-slide-img-mobile' ); //use custom set size
    

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

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

发布评论

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