跳转至主要内容

获取Thumbnial的URL

显示WordPress特色图像(Featured Image)只需要调用the_post_thumbnail()函数,但这个函数默认显示的结构可能不符合我们的需求,所以需要一种方式可以直接获取特色图像或者说Thumbnail的URL地址。 获取Thumbnail的URL 在Loop中获取
$img_id = get_post_thumbnail_id();
$img_url = wp_get_attachment_image_src($img_id);
$img_url = $img_url[0];
echo $img_url;
在Loop外部获取
$img_id = get_post_thumbnail_id(35); // 35 being the ID of the Post
$img_url = wp_get_attachment_image_src($img_id);
$img_url = $img_url[0];
获取Thumbnail的宽高 如果你需要知道图片的高度和宽度,可以这样
$img_width = $img_url[1]; // Get the width
$img_height = $img_url[2]; // Get the height
这里主要用到了wp get attachment image src()函数,你可以到WordPress官方的Codex了解更多关于该函数的信息

写回复

你的电子邮件地址不会被公开。

还没有评论