WordPress默認的Excerpt排版格式有些不盡人意,首先它默認的輸出字數是55,不支持HTML標簽,也就是輸出的內容不會換行,都是一大長段 此外JavaScript也無法被剝離出來。嚴重影響版面的美觀性,除非是手動錄入內容。
我們要做的就是讓自動提取的Excerpt內容非手動輸入,顯示自定義的排版格式。實現方法如下
WordPress默認摘錄的功能是在wpincludesformatting這個文件里,我們要修改的只有主題functions文件,請把下面的代碼加入到functions文件中
- remove_filter'get_the_excerpt','wp_trim_excerpt';
- add_filter'get_the_excerpt','improved_trim_excerpt';
- functionimproved_trim_excerpt$text{
- global$post;
- if''==$text{
- $text=get_the_content'';
- $text=apply_filters'the_content',$text;
- $text=str_replace'/]/]/',']]',$text;
- $text=preg_replace'@script[^] ?. ?script@si','',$text;
- $text=strip_tags$text,'';
- $excerpt_length=80;
- $words=explode'',$text,$excerpt_length+1;
- ifcount$words$excerpt_length{
- array_pop$words;
- array_push$words,'[...]';
- $text=implode'',$words;
- } //Vevb.com
- }
- return$text;
- }
這段代碼中是將wpincludesformatting里的
wp_trim_excerpt
改為了
improved_trim_excerpt
修改內容輸出的字數
$excerpt_length=80;
讓內容支持HTML標簽
$text=strip_tags$text,'';
如果想加入更多的HTML標簽,請在的后面緊隨著加入。
刪除不需要的JavaScript代碼
$text=preg_replace'@script[^] ?. ?script@si','',$text;
新聞熱點
疑難解答
圖片精選