利用pbootcmsV2.0.6新增的個人擴展標簽定制一個每日一圖
作者:云趣科技
時間:2020-04-03 13:57:59
瀏覽:3288
點贊:433
自PbootCmsV2.0.6開始,pbootcms支持自定義標簽,且升級不被覆蓋。媽媽再也不用擔心我的代碼升級被覆蓋啦。
于是就想到用這個功能定制一個每日一圖。
這個文件位置在 home下ExtLabelController控制器。
話不多說,先找圖源。度娘告訴我必應搜索就有每日一圖的API。
地址是這個:
//www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1
獲取到的是一串JSON,正和我意。
用pbootcms自帶的get_url方法直接抓取一下就搞定。
上代碼:
//抓取必應每日一圖 public function getBingImage(){ $url = '//www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'; $data = json_decode(get_url($url)); $result = $data->images[0]; $image = '//www.bing.com'.$result->url; return $image; }
然后再給自己定制一個標簽就好啦。
// 擴展單個標簽 private function diylabel() { $this->content = str_replace('{pboot:walle}', $this->getBingImage(), $this->content); }
最后只要在模板文件中寫上 {pboot:walle} 就調用出圖片地址。把這個放在 <img> 標簽中,或者放在 background 中,至此,搞定。
下面是整個ExtLabelController文件的代碼:
<?php /** * @copyright (C)2020-2099 Hnaoyun Inc. * @author XingMeng * @email * @date 2020年3月8日 * 個人擴展標簽可編寫到本類中,升級不會覆蓋 */ namespace app//home//controller; /* 此處雙斜杠需要換成單反斜杠*/ use core//basic//Controller; /* 此處雙斜杠需要換成單反斜杠*/ class ExtLabelController { protected $content; /* 必備啟動函數 */ public function run($content) { // 接收數據 $this->content = $content; // 執行個人自定義標簽函數 $this->diylabel(); // 返回數據 return $this->content; } // 擴展單個標簽 private function diylabel() { $this->content = str_replace('{pboot:walle}', $this->getBingImage(), $this->content); } //抓取必應每日一圖 public function getBingImage(){ $url = '//www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'; $data = json_decode(get_url($url)); $result = $data->images[0]; $image = '//www.bing.com'.$result->url; return $image; } }
最終效果,請移步首頁banner圖欣賞最終效果。
文章鏈接://bilinxiyi.cn/57.html
文章標題:利用pbootcmsV2.0.6新增的個人擴展標簽定制一個每日一圖
文章版權:PB模板網 (bilinxiyi.cn) 所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!
原文作者:云趣科技 | //www.a8yun.com/cms/46.ht
本文最后更新發布于
文章標題:利用pbootcmsV2.0.6新增的個人擴展標簽定制一個每日一圖
文章版權:PB模板網 (bilinxiyi.cn) 所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!
原文作者:云趣科技 | //www.a8yun.com/cms/46.ht
本文最后更新發布于
2022-11-28 18:03:21
,某些文章具有時效性,若有錯誤或已失效,請在下方留言或聯系客服: