[英]Facebook Meta Tag PHP condition
我在嘗試獲取此代碼段以為數組中的每個圖像輸出單獨的og:image元標記時遇到問題。
<?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];
}
?>
“ />
我仍然不知道。 有任何想法嗎?
試試這個功能。 它將返回圖像源數組。 然后,您可以始終將數組中的第一個圖像用作默認圖像。
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">';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.