2012-04-17 29 views
0

배열의 각 이미지에 대해 별도의 og : image 메타 태그를 출력하는 코드 스 니펫을 얻으려고하면 문제가 있습니다.Facebook 메타 태그 PHP 상태

<?php function catch_that_image() { 
global $post, $posts; 
$first_img = ''; 
ob_start(); 
ob_end_clean(); 
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); 
$first_img = $matches [1] [0]; 
if(empty($first_img)){ 
     //Defines a default image 
     $first_img = "http://example.com/images/fbthumb.jpg"; 
    } 
return $first_img; 
} 
?> 

현재 코드는 첫 번째 일치의 img src에 태그를 반환하지만 사용하는 공유 이미지를 선택할 수 있도록 나는 그것이 별도의 태그로 모두를 반환하고 싶습니다.

$first_img = $matches [1] [0]; 

가 필요

각 조건의 몇 가지 유형으로 변경하지만 어떻게 임 확실하지 될 :

나는이 줄을 알고있다. 어떤 도움이라도 대단히 감사하겠습니다. 감사!

편집 :

여기

있는 마지막 제안 후 내 코드 :

<?php function catch_that_image() { 
global $post, $posts; 
$result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches); 

return $matches[1]; 

}

>

"출력 />

나는 여전히 질수 그림을? 어떤 아이디어?

+0

' * * # i', $ post-> post_content, $ matches); foreach ($ matches [1] as $ match) { $ imgSources [] = $ match; } return $ imgSources; } > <메타 속성 = "OG : 이미지"내용 = "?"/> ' – brianr1

답변

0

이 기능을 사용해보십시오. 이미지 소스 배열을 반환합니다. 그런 다음 항상 배열의 첫 번째 이미지를 기본 이미지로 사용할 수 있습니다. 기능을 사용

function catch_that_image() { 
    global $post, $posts; 
    $imgSources = array(); 
    $result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches); 

    foreach($matches[1] as $match) { 
     $imgSources[] = $match; 
    } 

    return $imgSources; 
} 

또는 간단

function catch_that_image() { 
    global $post, $posts; 
    $result = preg_match_all('#<img.+src=[\'"]([^\'"]+)[^>]*>#i', $post->post_content, $matches); 

    return $matches[1]; 
} 

:

$imageArray = catch_that_image(); 
foreach($imageArray as $image) { 
    echo '<meta property=​"og:​image" content=​"$image">'; 
} 
+0

괜찮 그래서 마지막으로 u를 사용했는데 아직 값을 반환하지 않았습니다. 이 코드에 코드를 추가하는 방법을 알 수 없으므로 원래 게시물에 편집을 추가하십시오. – brianr1

+0

방화 광에서 다음을 반환합니다. brianr1