文章目录
特色图像是 wordpress 主要的文章缩略图功能,几乎全部 wordpress 模板都使用或支持特色图像。今天介绍的 wp_get_attachment_image_src()函数就是获取文章特色图像路径的 wordpress 函数,通过该函数可以返回一个包含图片路径、宽度和高度的有序数组。
函数结构:
<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>
返回值:
Array([0] => url //图片地址
[1] => width //图片宽度
[2] => height //图片高度
[3] => boolean //true表示返回了缩放后的图片,false表示返回了原始图片
)
参数说明:
$attachment_id – 数值,必需,想要获取信息的附件 ID,默认值:None
$size – 字符串,获取指定尺寸的图像,默认值:None,可选 thumbnail, medium, large or full(对应后台——设置——多媒体的图像大小 ,full 表示原始尺寸),或通过 array(300,240)自定义图片大小。需要注意的是 WordPress 2.5 以前的版本是没有 $size 参数的,只显示原始尺寸。
$icon – 布尔,可选,是否使用媒体图标来表示当前附件,比如:如果要获取的附件不是一张图片,而是,比如说视频,让$icon 为真可以返回代表视频(mime type:video)的图标,否则只能返回空值。这些代表不同 mime type 的图片在 wp-includes/images/crystal 目录下
简单示例:
1、获取文章缩略图 url 地址:
<?php$getThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()),full);
echo $getThumbnail[0];
?>
2、官方示例:
<?php$attachment_id = 8; // 附件ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // 返回一个数组
if( $image_attributes ) {
?><img src="<?php%20echo%20$image_attributes[0];%20?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
<?php } ?>
函数位置:
wp_get_attachment_image_src()位于 wp-includes/media.php
| 服务范围 | 1、专业提供WordPress主题、插件汉化、优化、PHP环境配置等服务请详询在线客服 | |||
| 2、本站承接 WordPress、DedeCMS、ThinkPHP 等系统建站、仿站、开发、定制等服务 | ||||
| 3、英文模板(主题)安装费用为120元/次,汉化主题首次免费安装(二次安装30元/次) | ||||
| 售后时间 | 周一至周五(法定节假日除外) 10:00-22:00 | |||
| 免责声明 | 本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: [email protected]),我们会及时删除,给您带来的不便,我们深表歉意! | |||
.demo tbody td a strong{color: #666;}.button_nowbuy,.button_demo {margin: 0em auto;border-style: none;border-width: 0px;padding: 0px;}#qrcode_en,#qrcode_cn{width: 130px;left: 10%;bottom: 250%;}a.button_demo:hover + #qrcode_en{visibility:visible;opacity: 1;bottom: 180%;-webkit-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;transition: all .4s ease-in-out;}a.button_demo:hover + #qrcode_cn{visibility:visible;opacity: 1;bottom: 180%;-webkit-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;transition: all .4s ease-in-out;}.table.demo{display: none;}





