2017-09-05 11 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画像がサーバー上に存在しない場合、私は元の形式にフォールバックする必要があります。私は効率的な方法でこれを行うことがどのように任意のアイデアが

$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); 

:私はWEBPのバージョンは、次のコードが使用しているかどうかをテストするには、次のように使用できるさまざまなページのための私のテンプレートファイルで

いいえお返事

答えて

0

フィルタの既存の.webpイメージを確認できますか?

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

はいの場合、イメージパスを元に戻す必要はありません。

関連する問題