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
背景,而只是被裏面。
只是爲了確認,這會將最後一幅圖像保存在什麼字符串中? (不知道我是否正確地問) – user3550879
其get_the_content()中的url列表並將它們存儲在$ images中, 第二行是拔出最後一張圖片。 – itzikb