洞悉互聯(lián)網(wǎng)前沿資訊,探尋網(wǎng)站營銷規(guī)律
作者:狐靈科技 | 2019-12-13 11:46 |點擊:
默認(rèn)情況下織夢文章內(nèi)容中只能一張圖片作為縮略圖出現(xiàn)在文章類表頁面或者文章首頁。但是有的時候我們在織夢模板的制作過程中,用戶想用調(diào)用顯示多張(例如3張,或者5張)縮略圖顯示在類表頁面,我們需要如何去實現(xiàn)呢?代碼又要如何寫呢?
實現(xiàn)方法如下:
首先我們找到include下的extend.func.php 文件,在里面加入如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * 文章內(nèi)容提取圖片(多張)自定義輸出 * * @access public * @param string $string 文檔內(nèi)容 * @param string $num 輸出幾張 * @return string */ if (!function_exists( 'getBodypics' )) { function getBodypics( $string , $num ) { preg_match_all( "/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/" , $string , $matches ); $imgsrc_arr = array_unique ( $matches [3]); $count = count ( $imgsrc_arr ); $i = 0; foreach ( $imgsrc_arr as $imgsrc ) { if ( $i == $num ) break ; $result .= "<a><img src=\"$imgsrc\"/></a>" ; $i ++; } return $result ; } } |
然后,我們再在arclist標(biāo)簽中調(diào)用,代碼寫法如下:
1
|
[field:body function=getBodypics(@me,3)/] |
在這里需要注意的是:arclist標(biāo)簽中首先要 addfields='body' 否則無法調(diào)用到body。大家可以去嘗試看看。