Focus on cloud service provider activities
Notes on website operation and maintenance

Code of Typecho program calling article thumbnail

If we are using Typecho to develop theme functions for the second time, we may need to use the call content image as a thumbnail.

 /**Output article thumbnail laozuo.org*/ function showThumbnail($widget) {  //Default thumbnail when the article has no picture $rand = rand(1,5); //  Random 1-5 thumbnails $random = $widget->widget('Widget_Options')->themeUrl . '/img/sj/' . $rand . '.jpg'; //  Random thumbnail path // $random = $widget->widget('Widget_Options')->themeUrl . '/img/laozuo.jpg'; //  If you want only one default thumbnail, please delete the "//" at the beginning of the line $attach = $widget->attachments(1)->attachment; $pattern = '/\<img.*? src\=\"(.*?)\"[^>]*>/i';  if (preg_match_all($pattern, $widget->content, $thumbUrl)) { echo $thumbUrl[1][0]; } else     if ($attach->isImage) { echo $attach->url;  } else { echo $random; } }

Add function code to the definition file page. Of course, you need to modify the URL of the default image. And prepare five random graphs.

 <?php%20showThumbnail($this);% 20?>

Call the image. If there is no thumbnail, the default image will be called.

Vote for you
Domain name host preferential information push QQ group: six hundred and twenty-seven million seven hundred and seventy-five thousand four hundred and seventy-seven Get preferential promotion from merchants.
Like( zero )
Do not reprint without permission: Lao Zuo's Notes » Code of Typecho program calling article thumbnail