同分類文章列表調用
{foreach GetList(調用條數,分類ID) as $related}<li><span>{$related.Time('Y-m-d')}</span><a href="{$related.Url}">{$related.Title}</a></li>{/foreach}獲取大目錄下的所有文章(包括子目錄文章),將上面的GetList改為:
Getlist(調用條數,分類ID,null,null,null,null,array('has_subcate'=>true));調用其它數據參考文章標簽,文章標簽。
注意:此處需要使用
foreach
循環中as后面變量名,如案列中使用的$related,如需調用標題則用 {$related.Title}
,而并非是 {$article.Title}
。網站關鍵詞、描述添加
{if $type=='article'} <title>{$title}_{$article.Category.Name}_{$name}</title> {php} $aryTags = array(); foreach($article->Tags as $key){ $aryTags[] = $key->Name; } if(count($aryTags)>0){ $keywords = implode(',',$aryTags); } else { $keywords = $zbp->name; } $description = preg_replace('/[\r\n\s]+/', ' ', trim(SubStrUTF8(TransferHTML($article->Content,'[nohtml]'),135)).'...'); {/php} <meta name="keywords" content="{$keywords}"/> <meta name="description" content="{$description}"/> <meta name="author" content="{$article.Author.StaticName}">{elseif $type=='page'} <title>{$title}_{$name}_{$subname}</title> <meta name="keywords" content="{$title},{$name}"/> {php} $description = preg_replace('/[\r\n\s]+/', ' ', trim(SubStrUTF8(TransferHTML($article->Content,'[nohtml]'),135)).'...'); {/php} <meta name="description" content="{$description}"/> <meta name="author" content="{$article.Author.StaticName}"> {elseif $type=='index'} <title>{$name}{if $page>'1'}_第{$pagebar.PageNow}頁{/if}_{$subname}</title> <meta name="Keywords" content="自定義關鍵詞,{$name}"> <meta name="description" content="自定義描述_{$name}_{$title}"> <meta name="author" content="{$zbp.members[1].StaticName}">{else} <title>{$title}_{$name}_第{$pagebar.PageNow}頁</title> <meta name="Keywords" content="{$title},{$name}"> <meta name="description" content="{$title}_{$name}_當前是第{$pagebar.PageNow}頁"> <meta name="author" content="{$zbp.members[1].StaticName}">{/if}
評論數判斷
{if $article.CommNums==0}暫無留言{elseif $article.CommNums==1}僅有1條留言{else}已有{$article.CommNums}條留言{/if}
頁面判斷
{if $type=='index'&&$page=='1'} /*判斷首頁*/{if $type=='category'} /*判斷分類頁*/{if $type=='article'} /*判斷日志頁,不含獨立頁面,{if $article.Type==ZC_POST_TYPE_ARTICLE}(另一方案)*/{if $type=='page'} /*判斷獨立頁面*/{if $type=='author'} /*判斷用戶頁*/{if $type=='date'} /*判斷日期頁*/{if $type=='tag'} /*判斷標簽頁*/
示例:首頁和分類列表頁分離
在index.php文件里作判斷,分離模板。比如:
{if $type=='index'&&$page=='1'} {template:c_index}{else}{template:c_list}{/if}然后新建兩個相應的模板文件:c_index.php和c_list.php
隨機獲得文章中的四張圖片中的一張
適用于多圖站點,在模板文件中使用:
{php}$temp=mt_rand(1,4);$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";$content = $article->Content;preg_match_all($pattern,$content,$matchContent);if(isset($matchContent[1][0])) $temp=$matchContent[1][0];else$temp=$zbp->host."zb_users/theme/$theme/style/images/random/$temp.jpg";//需要在相應位置放置4張jpg的文件,名稱為1,2,3,4{/php} <img src="{$temp}" />
僅顯示文字的文章簡介調用方法
{SubStrUTF8(TransferHTML($article.Intro,"[nohtml]"),200)}
分類目錄面包屑的代碼編寫
{php}$html='';function navcate($id){ global $html; $cate = new Category; $cate->LoadInfoByID($id); $html ='>> <a href="' .$cate->Url.'" title="查看' .$cate->Name. '中的全部文章">' .$cate->Name. '</a> '.$html; if(($cate->ParentID)>0){navcate($cate->ParentID);}}navcate($article->Category->ID);global $html;echo $html;{/php}
顯示文章的相關文章
方法一和方法二的區別:方法一是通過匹配tags,然后按照更新時間列出相關文章;方法二,同樣通過匹配tags,然后隨機列出相關,但隨機比較消耗資源,并且不支持SQLite數據庫。
方法1
使用getlist,請參考GetList 函數 例5
友好的時間顯示
function TimeAgo( $ptime ) { $ptime = strtotime($ptime); $etime = time() - $ptime; if($etime < 1) return '剛剛'; $interval = array ( 12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')', 30 * 24 * 60 * 60 => '個月前 ('.date('m-d', $ptime).')', 7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')', 24 * 60 * 60 => '天前', 60 * 60 => '小時前', 60 => '分鐘前', 1 => '秒前' ); foreach ($interval as $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . $str; } };}友好時間在編譯模板文件中使用方法
{TimeAgo($XXX.Time())}
例:{TimeAgo($article.Time())}
判斷是否為手機端
function APPID_is_mobile() { if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $is_mobile = false; } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.) || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) { $is_mobile = true; } else { $is_mobile = false; } return $is_mobile;}將以上函數保存到主題include.php 在模板文件中使用方法
{if APPID_is_mobile()}
手機端代碼
{else}
PC端代碼
{/if}
自動給IMG添加alt屬性
搜索引擎對網站圖片SEO的友好性很大取決于alt屬性,使用此函數可以自動為img添加alt屬性為文章標題! 首先在include.php加入此函數:
function APPID_imgalt(&$template){ global $zbp; $article = $template->GetTags('article'); $pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>/i"; $replacement = '<img alt="'.$article->Title.'" src=$2$3.$4$5/>'; $content = preg_replace($pattern, $replacement, $article->Content); $article->Content = $content; $template->SetTags('article', $article);}然后掛載如下接口:
Add_Filter_Plugin('Filter_Plugin_ViewPost_Template','APPID_imgalt');
版權聲明:若文中沒有特別聲明皆為原創文章,轉載時請以鏈接形式注明文章出處與原文鏈接。
非書面授權,禁止轉載。本作品采用 CC BY-NC-ND/2.5/CN 許可協議。
如果幫您解決了問題,可以給小編打賞,小編不抽煙不喝酒,6元就夠吃個泡面了,感激不盡。
非書面授權,禁止轉載。本作品采用 CC BY-NC-ND/2.5/CN 許可協議。
如果幫您解決了問題,可以給小編打賞,小編不抽煙不喝酒,6元就夠吃個泡面了,感激不盡。