【发布时间】:2017-09-05 06:21:19
【问题描述】:
所以我有一些代码可以修改 wordpress 中的the_content() 过滤器。
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
$patterns = array("/.jpg/", "/.jpeg/", "/.png/");
if (isset($_COOKIE['webpsupport'])) // generated through modernizr detect webp
$content = preg_replace($patterns, "_result.webp", $content);
return $content;
}
return $content;
}
现在这可以工作并将图像转换为 .webp 格式,但如果服务器上不存在 .webp 图像,我需要回退到原始格式。
在我的各个页面的模板文件中,我可以使用以下代码来测试 webp 版本是否存在:
$image = get_the_post_thumbnail_url($postId);
$ext = pathinfo($image, PATHINFO_EXTENSION);
$thePostThumbPath = str_replace("http://localhost", "", $thePostThumbUrl);
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $thePostThumbPath))
$thePostThumbUrl = str_replace("_result.webp", "." . $ext, $thePostThumbUrl);
任何想法我可以如何有效地做到这一点。
干杯
【问题讨论】:
标签: wordpress hook fallback webp