2016-07-29 98 views
0

我有以下代碼,它將最後一張圖像包裝在#last-img div的wordpress文章中。通過php將圖像轉換爲背景圖像

HTML

<?php 
    preg_match_all('/(<img [^>]*>)/', get_the_content(), $images); 

    for($i=0; isset($images[1]) && $i < count($images[1]); $i++) { 
     if ($i == end(array_keys($images[1]))) { 
      echo sprintf('<div id="last-img">%s</div>', $images[1][$i]); 
      continue; 
     } 

     echo $images[1][$i]; 
    } 
?> 

CSS

#last-img { ... } 

我想要的形象是#last-img背景,而只是被裏面。

回答

1

我不得不改變u chosed使用模式,

穿行整個陣列上的U可以檢測在下面的代碼在var最後一個數組的instand:

<?php 
    preg_match_all('/src="([^"]*)"/i', get_the_content(), $images); 
    echo $images[1][count($images[1])-1]; 
?> 

你可以請隨意使用「img src」。 GL。

+0

只是爲了確認,這會將最後一幅圖像保存在什麼字符串中? (不知道我是否正確地問) – user3550879

+0

其get_the_content()中的url列表並將它們存儲在$ images中, 第二行是拔出最後一張圖片。 – itzikb