\extend\function.php 下面增加以下代碼
if (!function_exists('diy_content_img')) {
/**
* 獲得文檔正文中的圖片
*/
function diy_content_img($aid = '', $number = '0', $field = 'content')
{
//文檔ID
if (!$aid) return false;
//判斷條數存在且不為數字的情況下
if($number && !is_numeric($number)) $number = 0;
//根據aid獲取欄目id
$channel = db('archives')->where('aid', $aid)->value('channel');
if (!$channel) return false;
//根據模型ID獲取模型表
$table = db('channeltype')->where('id', $channel)->value('table');
if (!$table) return false;
//拼接附加表
$table = $table . '_content';
//根據模型表獲取正文內容
$content = db($table)->where('aid', $aid)->value($field);
//轉義HTML
$content = htmlspecialchars_decode($content);
//正則獲取圖片
$pattern = '/<img.*?src=["\']?(.*?)["\'].*?>/i';
preg_match_all($pattern, $content, $matches);
if (!empty($matches[1])) {
$data = array();
$count = count($matches[1]);
for ($i = 0; $i < $count; $i++) {
if ($number && $i >= $number) {
break;
}
$data[] = array(
'src' => $matches[1][$i]
);
}
return $data;
}
return false;
}
}
內容頁調用
{eyou:volist name="(diy_content_img($eyou['field']['aid'],10))" id='field'}
<img src="{$field.src}" height="80" width="80">
{/eyou:volist}
循環調用
{eyou:volist name="(diy_content_img($field['aid']))" id='field1'}
<img src="{$field1.src}" height="80" width="80">
{/eyou:volist}
支持傳參,第一個文章ID,第二個條數,默認為0=所有,第三個=字段,默認為content
diy_content_img($field['aid'])
自定義調用,比如我后臺的編輯器字段為 bianjiqi,然后我就想要三張圖
diy_content_img($field['aid'],3,'bianjiqi')
支持動態,偽靜態,靜態,任意模型,如果有什么問題可以留言聯系!