如果浏览器支持使用 get_the_post_thumbnail() 获取 webp 图片

Get webp image if browser supports it using get_the_post_thumbnail()

所以我想在一个网站上实现webp。 目前我正在使用 get_the_post_thumbnail() 函数来获取 post 特色图片。 有没有办法用这个函数获取webp版本的图片

我试过使用 wp-webp plugin 但它不起作用。

干杯

好的,我能想到解决这个问题的唯一方法是检查浏览器并提供 .webp 图像(如果浏览器支持),方法是将文件扩展名替换为 .webp

$postId = $post->ID;
$patterns = array("/.jpg/", "/.jpeg/", "/.png/");
$thePostThumbUrl = get_the_post_thumbnail_url($postId, array(768,768));
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') || strpos($_SERVER['HTTP_USER_AGENT'], 'OPR/') || strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') && !strpos($_SERVER['HTTP_USER_AGENT'], 'Edge')) 
    $thePostThumbUrl = preg_replace($patterns, ".webp", $thePostThumbUrl);