explain : This code is applicable to the theme making partners, because there are four display modes of the head image set in the background appearance settings. If it is a theme for yourself, you can greatly reduce the code volume on this basis.

If you just write a topic for yourself, you can refer to Typecho Output thumbnails as required

Code function

This code can realize the following functions:

  1. When the homepage header map switch is turned off, the thumb field is the image address, and the article header map can still be displayed
  2. When the article header switch is turned off, the thumb field is the image address, and the homepage header can still be displayed
  3. When the header of the first page of the article is closed, the thumb field is the image address, and the header of the first page of the article can be displayed
  4. When the header switch is turned on, the thumb field is no, and the current article will not display the header
  5. When the head map switch is turned on, you can press Order of four selections Select display head image

Here, there are several unreasonable requirements that are no longer implemented in the code, because the following requirements will destroy the logic of the code:

  1. When the homepage article header switch is turned off, the thumb field is the image address. You want to display only the header of the current article, or only the homepage header of the current article
  2. When the homepage header map switch is turned off, the homepage header map will be displayed for the current article.

In general, the head map switch in the background settings has the highest weight. You cannot allow the thumb field to operate beyond its authority. Otherwise, it will cause some bugs and sacrifice some functions.

code analysis

At the beginning, when I was preparing to write the logic, I drew a big picture:

The final result is: I am more and more confused!

Then, the following nodes are adjusted and streamlined as follows:

The final code is the code written in the logic of the second figure.

The thumb field has the highest weight. Even if the display header switch is turned off, as long as the thumb field is an image address, the header can still be displayed (the second function)

Reference code

 public static function whenSwitchHeaderImgSrc($widget){ $options = mget(); $rand = rand(1,$options->RandomPicAmnt); $howToThumb = $options->RandomPicChoice; $random = $widget->widget('Widget_Options')->themeUrl . '/ img/sj/' . $rand . '.jpg'; $pattern = '/\<img.*? src\=\"(.*?)\"[^>]*>/i'; $patternMD = '/\!\ [.*?\]\((http(s)?:\/\/.*? (jpg|png))/i'; $patternMDfoot = '/\[.*?\]:\s*(http(s)?:\/\/.*? (jpg|png))/i'; //Random thumbnail path //Regularly match pictures of/images/sj/under the theme directory (named in numerical order) $attach = $widget->attachments(1)->attachment;// Picture in attachment $thumbField = $widget->fields->thumb; if ($howToThumb == '0'){ return $random; }elseif ($howToThumb == '1' || $howToThumb == '2'){ if (! empty($thumbField)){ return $thumbField; }elseif (isset($attach->isImage) && $attach->isImage == 1){ return $attach->url; }else{ if (preg_match_all($pattern, $widget->content, $thumbUrl)){ $thumb = $thumbUrl[1][0]; }elseif (preg_match_all($patternMD, $widget->content, $thumbUrl)){ $thumb = $thumbUrl[1][0]; }elseif (preg_match_all($patternMDfoot, $widget->content, $thumbUrl)){ $thumb = $thumbUrl[1][0]; }Else {//There are no pictures in the article if ($howToThumb == '1'){ return ''; }else{ return $random; } } return $thumb; } }elseif ($howToThumb == '3'){ if (! empty($thumbField)){ return $thumbField; }else{ return $random; } } } //Output header image address public static function returnHeaderImgSrc($widget,$select){ $options = mget(); $thumbField = $widget->fields->thumb; If (strtoupper ($thumbField)=="NO") {//thumb is no and the header is not displayed directly $imgSrc = ""; }Else {//thumb is not no if (in_array('NoRandomPic-post',$options->indexsetup) && in_array('NoRandomPic-index', $options ->indexsetup) {//Close all if ($thumbField !=  ""){ $imgSrc = Content::whenSwitchHeaderImgSrc($widget); }else{ $imgSrc = ""; } }else if (!in_array('NoRandomPic-post',$options->indexsetup) && ! in_array('NoRandomPic-index', $options ->indexsetup) {//Enable all $imgSrc = Content::whenSwitchHeaderImgSrc($widget); }Else {//Open and close If (in_array ('NoRandomPic post ', $options ->indexsetup)) if ($select == "post"){ $imgSrc = ""; }else{ $imgSrc = Content::whenSwitchHeaderImgSrc($widget); } }Else {//Do not display the header image of the home page, but display the header image of the article page if ($select == "post"){ $imgSrc = Content::whenSwitchHeaderImgSrc($widget); }else{ $imgSrc = ""; } } } } return $imgSrc; }
Last modification: February 26, 2019
Do you like my article?
Don't forget to praise or appreciate, let me know that you accompany me on the way of creation.