2013-02-19 28 views
0

抓住並調整大小的縮略圖,我使用旱廁的大小調整從這個鏈接:如何使用功能圖像和第一張圖片縮略圖?

https://github.com/sy4mil/Aqua-Resizer

呼叫縮略圖與環路這段代碼顯示:

<?php $thumb = get_post_thumbnail_id(); 
$img_url = wp_get_attachment_url($thumb,'full'); 
$image = aq_resize($img_url, 150, 700, true); 
?> 
<img src="<?php echo $image ?>" width="150" height="700" alt="<?php the_title(); ?>"/> 

它運作良好。但僅限於特徵圖像。 我想將調用者設置爲不是用於特徵圖像,而是用於第一個發佈圖像。 因此,當我忘記在我的帖子上設置功能圖片時,第一張圖片將顯示爲縮略圖。

我知道代碼的流動應該是這樣的:

if(has_post_thumbnail()) { 
// resize post thumbnail here e.g. $img_url = aq_resize... 
} elseif($first_img) { 
// resize the first img here, $img_url = aq_resize($first_img, ... 
} else { 
// $img_url = ''; //empty 
} 

但我新上的PHP。任何人都可以幫忙嗎? 在此先感謝

+0

「通過編輯器添加的第一張圖像中的第一張圖像」? – kjetilh 2013-02-19 11:26:01

回答

2

你可以把這個函數放在你的functions.php中,然後從任何地方調用它。它會返回它在您的帖子中找到的第一個圖片標籤的source屬性,或者如果它沒有找到任何內容,則返回空白字符串。

function get_first_image_src() 
{ 
    $content = get_the_content(); 
    $image_regex = "/<img [^>]*src=[\"']([^\"^']*)[\"']/"; 
    preg_match($image_regex, $content, $match); 

    if (empty($match)) 
     return ""; 

    return $match[1]; 
} 
相關問題