It is popular to pick the first image in the article as a list page thumbnail, and the general theme in WordPress is the default, so let's take a look at the code example for the first picture of each article in a Web site in PHP.
<?php $temp =mt_rand (1,4); $pattern = "/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg|\.png])) [\ ' |\ '].*? [\/]?>/]; $content = $article->content; Article content Preg_match_all ($pattern, $content, $matchContent); if (Isset ($matchContent [1][0])) { $temp = $matchContent [1][0];} else{ $temp = "images/random/$temp. jpg";//need to place 4 jpg files in the corresponding location, named 1,2,3,4} ?>
The above code by default calls the first picture of the article, when the article does not have a picture, random call theme style/images/random/under the 1.jpg, 2.jpg, 3.jpg, 4.jpg pictures. If you don't want to call a random picture, you can modify it:
<?php $pattern = "/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg|\.png])) [\ ' |\ '].*? [\/]?>/]; $content = $article->content; Article content Preg_match_all ($pattern, $content, $matchContent); if (Isset ($matchContent [1][0])) { $temp = $matchContent [1][0];} else{ $temp = "./images/no-image.jpg";//a JPG image named No-image is placed in the corresponding location} ?>
Call the first picture of the article, if the article does not have a picture to call the default picture no-image.jpg
Summary: The above is the entire content of this article, I hope to be able to help you learn.