2017-09-05 15 views
0

그래서 the_content() 필터를 수정할 수있는 코드가 있습니다.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); 

나는 효율적인 방법으로이 작업을 수행 할 수있는 방법 어떤 아이디어가.

환호

답변

0

필터의 기존 .webp 이미지를 확인할 수 있습니까?

$patterns = array("/.jpg/", "/.jpeg/", "/.png/"); 
if (isset($_COOKIE['webpsupport']) && $webpExists) 
    $content = preg_replace($patterns, "_result.webp", $content); 

그렇다면 이미지 경로를 되돌릴 필요가 없습니다.