Php Wordpress`获取文章缩略图'源URL

Php Wordpress`获取文章缩略图'源URL,php,wordpress,Php,Wordpress,这将返回一个完全格式化的HTML 我为附件尝试了一些其他WP函数,但由于某些原因,这些函数没有返回任何有用的内容 我将接受一个WP函数或一个解析器,它将提取src标记的值 $thumb_id = $post->ID; $pictureLink = get_the_post_thumbnail($thumb_id, 'medium'); 更新: 将提取src标记值的解析器 $thumb_id = $post->ID; $pictureLink = get_the_post_thu

这将返回一个完全格式化的HTML

我为附件尝试了一些其他WP函数,但由于某些原因,这些函数没有返回任何有用的内容

我将接受一个WP函数或一个解析器,它将提取
src
标记的值

$thumb_id = $post->ID;

$pictureLink = get_the_post_thumbnail($thumb_id, 'medium');
更新: 将提取
src
标记值的解析器

$thumb_id = $post->ID;

$pictureLink = get_the_post_thumbnail($thumb_id, 'medium');
您可以使用:

$doc = new DOMDocument();
$doc->loadHTML($pictureLinkHtml);
$xpath = new DOMXPath($doc);
$pictureLink = $xpath->evaluate("string(//img/@src)");

请参阅参考资料。

您可以比解析它简单得多。首先,您需要从post ID获取附件ID。然后您可以获取包含URL、宽度和高度的源。这是


@酒吧什么不管用?这是经过测试并证明有效的。它没有返回正确的url。不知道为什么会出现差异。
get\u post\u thumbnail\u id
需要post id,因此请确保提供了该id,然后检查缩略图是否足够大,或者删除它,因为它是可选的。然后,您应该获得附加到帖子的缩略图。
$image_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src($image_id, 'large');

echo $image[0];