教程

Typecho的语法及函数,实在记不住,而且每次都要去百度一遍,着实是麻烦。还是自己保存下来吧,方便查阅!文章内容均来自网络,并整合。https://blog.chrison.cn/work/87.html模板常用函数1、站点网址<?php$this->options->title()?>2、站点网址<?php$this->options->siteUrl();?>3、完整路径标题如分享几个Type­cho中常用的调用函数<?php$this->archiveTitle('&raquo;',<spanclass="string">'','|');?><?php$this->options->title();?>4、站点说明<?php$this->options->description()?>5、模板文件夹地址<?php$this->options->themeUrl();?>6、导入模板文件夹内的php文件<?php$this->need('.php');?>7、文章或者页面的作者<?php$this->author();?>8、作者头像<?php$this->author->gravatar('40')?>此处输出的完整的img标签,40是头像的宽和高。9、该文作者全部文章列表链接<?php$this->author->permalink();?>10、该文作者个人主页链接<?php$this->author->url();?>11、该文作者的邮箱地址<?php$this->author->mail();?>12、上一篇与下一篇调用代码<?php$this->thePrev();?><?php$this->theNext();?>13、判断是否为首页,输出相关内容<?phpif($this->is('index')):?>//首页输出内容<?phpelse:?>//不是首页输出内容<?phpendif;?>14、文章或页面,评论数目<?php$this->commentsNum('NoComments','1Comment','%dComments');?>15、截取部份文章(首页每篇文章显示摘要),350是字数<?php$this->excerpt(350,'...');?>16、调用自定义字段(官方文档坑爹,竟然没有,博主自己摸索出来的)<?php$this->fields->fieldName?>17、RSS地址<?php$this->options->feedUrl();?>18、获取最新post<?php$this->widget('Widget_Contents_Post_Recent','pageSize=8&type=category')->parse('<li><ahref="{permalink}">{title}</a></li>');?>19、纯文字分类名称,不带链接<?php$this->category(',',false);?>20、获取当前文章所属分类(包含链接)<?phpif($this->is('post')):?><span><?php$this->category('');?></span><?phpendif;?>21、获取文章分类列表<ul><?php$this->widget('Widget_Metas_Category_List')->parse('<li><ahref="{permalink}">{name}</a>({count})</li>');?></ul>22、获取某分类post<ul><?php$this->widget('Widget_Archive@indexyc','pageSize=8&type=category','mid=1')->parse('<li><ahref="{permalink}"title="{title}">{title}</a></li>');?></ul>23、获取最新评论列表<ul><?php$this->widget('Widget_Comments_Recent')->to($comments);?><?phpwhile($comments->next()):?><li><ahref="<?php$comments->permalink();?>"><?php$comments->author(false);?></a>:<?php$comments->excerpt(50,'...');?></li><?phpendwhile;?></ul>24、首页获取最新文章代码限制条数<?phpwhile($this->next()):?><?phpif($this->sequence<=3):?>html<?phpendif;?><?phpendwhile;?>25、获取最新评论列表(只显示访客评论不显示作者或者说自己发的评论)<?php$this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments);?><?phpwhile($comments->next()):?><li><ahref="<?php$comments->permalink();?>"><?php$comments->author(false);?></a>:<?php$comments->excerpt(50,'...');?></li><?phpendwhile;?>26、获取文章时间归档<ul><?php$this->widget('Widget_Contents_Post_Date','type=month&format=FY')->parse('<li><ahref="{permalink}">{date}</a></li>');?></ul>27、获取标签集合,也就是标签云<?php$this->widget('Widget_Metas_Tag_Cloud','ignoreZeroCount=1&limit=28')->to($tags);?><?phpwhile($tags->next()):?><ahref="<?php$tags->permalink();?>"class="size-<?php$tags->split(5,10,20,30);?>"><?php$tags->name();?></a><?phpendwhile;?>28、调用该文相关文章列表<?php$this->related(5)->to($relatedPosts);?><?phpif($relatedPosts->have()):?>//这句也可以写成if(count($relatedPosts->stack))<ul><?phpwhile($relatedPosts->next()):?><li><ahref="<?php$relatedPosts->permalink();?>"title="<?php$relatedPosts->title();?>"><?php$relatedPosts->title();?></a></li><?phpendwhile;?></ul><?phpelse:?><li>无相关文章</li><?phpendif;?>29、隐藏head区域的程序版本和模版名称<?php$this->header("generator=&template=");?>30、获取读者墙<?php$period=time()-999592000;//時段:30天,單位:秒$counts=Typecho_Db::get()->fetchAll(Typecho_Db::get()->select('COUNT(author)AScnt','author','url','mail')->from('table.comments')->where('created>?',$period)->where('status=?','approved')->where('type=?','comment')->where('authorId=?','0')->group('author')->order('cnt',Typecho_Db::SORT_DESC)->limit(25));$mostactive='';$avatar_path='http://www.gravatar.com/avatar/';foreach($countsas$count){$avatar=$avatar_path.md5(strtolower($count['mail'])).'.jpg';$c_url=$count['url'];if(!$c_url)$c_url=Helper::options()->siteUrl;$mostactive.="<ahref='".$c_url."'title='".$count['author']."(参与".$count['cnt']."次互动)'target='_blank'><imgsrc='".$avatar."'alt='".$count['author']."的头像'class='avatar'width='32'height='32'/></a>\n";}echo$mostactive;?>31、登陆与未登录用户展示不同内容<?phpif($this->user->hasLogin()):?>//登陆可见<?phpelse:?>//未登录和登陆均可见<?phpendif;?>32、导航页面列表调用隐藏特定的页面这个演示隐藏了al­bum和search两个页面<ul><li<?phpif($this->is('index')):?>class="current"<?phpendif;?>><ahref="<?php$this->options->siteUrl();?>">主页</a></li><?php$this->widget('Widget_Contents_Page_List')->to($pages);?><?phpwhile($pages->next()):?><?phpif(($pages->slug!='album')&&($pages->slug!='search')):?><li<?phpif($this->is('page',$pages->slug)):?>class="current"<?phpendif;?>><ahref="<?php$pages->permalink();?>"title="<?php$pages->title();?>"><?php$pages->title();?></a></li><?phpendif;?><?phpendwhile;?></ul>参数说明:9.0版typecho支出在后台管理页面编辑时选择隐藏页面。33、Type­cho归档页面<?php$this->widget('Widget_Contents_Post_Recent','pageSize=10000')->to($archives);$year=0;$mon=0;$i=0;$j=0;$output='<divid="archives">';while($archives->next()):$year_tmp=date('Y',$archives->created);$mon_tmp=date('m',$archives->created);$y=$year;$m=$mon;if($mon!=$mon_tmp&&$mon>0)$output.='</ul></li>';if($year!=$year_tmp&&$year>0)$output.='</ul>';if($year!=$year_tmp){$year=$year_tmp;$output.='<h3class="al_year">'.$year.'年</h3><ulclass="al_mon_list">';//输出年份}if($mon!=$mon_tmp){$mon=$mon_tmp;$output.='<li><spanclass="al_mon">'.$mon.'月</span><ulclass="al_post_list">';//输出月份}$output.='<li>'.date('d日:',$archives->created).'<ahref="'.$archives->permalink.'">'.$archives->title.'</a><em>('.$archives->commentsNum.')</em></li>';//输出文章日期和标题endwhile;$output.='</ul></li></ul></div>';echo$output;?>34、获取当前文章页缩略图<?php$this->attachments(1)->attachment->url();?>务必注意,这里所谓缩略图指的是当前文章页第一个附件地址,请确保第一个附件类型为图片。35、根据页面类型显示内容判断是文章页则显示内容<?phpif($this->is('post')):?>想要显示的内容1<?phpendif;?>判断是页面则显示内容<?phpif($this->is('page','about')):?>想要显示的内容2<?phpendif;?>36、Title增加副标题<?phpif($this->is('index')):?>-副标题<?phpendif;?>37、按分类输出cms<divclass="row"><?php$this->widget('Widget_Metas_Category_List')->to($categories);?><?phpwhile($categories->next()):?><?phpif(count($categories->children)===0):?><?php$this->widget('Widget_Archive@category-'.$categories->mid,'pageSize=5&type=category','mid='.$categories->mid)->to($posts);?><divclass="col-lg-6"><divclass="panelpanel-default"><divclass="panel-heading"><iclass="glyphiconglyphicon-th"></i><ahref="<?php$categories->permalink();?>"class="guidang"id="posts-list-<?php$categories->slug();?>"><?php$categories->name();?></a><ahref="<?php$categories->permalink();?>"class="pull-right"id="posts-list-<?php$categories->slug();?>"><iclass="glyphiconglyphicon-option-horizontal"></i></a></div><divclass="panel-body"><?phpwhile($posts->next()):?><pclass="overflow"><ahref="<?php$posts->permalink();?>"title="<?php$posts->title(40);?>"><iclass="glyphiconglyphicon-chevron-right"></i><?php$posts->title(40);?></a></p><?phpendwhile;?></div></div></div><?phpelse:?><?phpendif;?><?phpendwhile;?></div>38、相关文章<?php$this->related(5)->to($relatedPosts);?><ul><?phpwhile($relatedPosts->next()):?><li><ahref="<?php$relatedPosts->permalink();?>"title="<?php$relatedPosts->title();?>"><?php$relatedPosts->title();?></a></li><?phpendwhile;?></ul>39、侧边栏热门标签<divclass="widget"><h3><?php_e('热门标签');?></h3><ulclass="cate"><?php$this->widget('Widget_Metas_Tag_Cloud',array('sort'=>'count','ignoreZeroCount'=>true,'desc'=>true,'limit'=>20))->to($tags);?><?phpwhile($tags->next()):?><li><arel="tag"href="<?php$tags->permalink();?>"><?php$tags->name();?></a></li><?phpendwhile;?><divclass="clear"></div></ul></div>40、侧边栏输出所有标签<divclass="widget"><h3><?php_e('所有标签');?></h3><ulclass="cate"><?php$this->widget('Widget_Metas_Tag_Cloud')->to($tags);?><?phpwhile($tags->next()):?><li><arel="tag"href="<?php$tags->permalink();?>"><?php$tags->name();?></a></li><?phpendwhile;?><divclass="clear"></div></ul></div>41、当前文章的标签说明:(',',true,'none')第一个单引号间的逗号代表标签与标签的间隔用逗号隔开,true是标签以超链接形式输出false则只输出文字,none为该文章没有标签时显示的提示信息可为空。<?php$this->tags(',',true,'none');?>引申用法:如果想给每个标签套上div或者span什么的就需要这要做<div><?php$this->tags('</div><div>',true,'none');?></div>判断当前标签并自定义内容<?phpif(in_array('abc',$this->tags)):?>文章含有abc标签就会显示这里<?phpendif;?>判断文章是否存在标签,如果存在输出标签,如果不存在输出该文章分类<?phpif(count($this->tags)==0):?>//此处的count也可改为size<?php$this->category(',',true,'none');?>//文章分类<?phpelse:?><?php$this->tags(',',true,'none');?>//文章标签<?phpendif;?>代码片段以下为代码片段及对应的调用方式;代码片段请放置于function.php中1、输出文章缩略图/**输出文章缩略图*/functionshowThumbnail($widget){//当文章无图片时的默认缩略图$rand=rand(1,5);//随机1-5张缩略图$random=$widget->widget('Widget_Options')->themeUrl.'/img/sj/'.$rand.'.jpg';//随机缩略图路径//$random=$widget->widget('Widget_Options')->themeUrl.'/img/mr.jpg';//若只想要一张默认缩略图请删除本行开头的"//"$attach=$widget->attachments(1)->attachment;$pattern='/\<img.*?src\=\"(.*?)\"[^>]*>/i';if(preg_match_all($pattern,$widget->content,$thumbUrl)){echo$thumbUrl[1][0];}elseif($attach->isImage){echo$attach->url;}else{echo$random;}}调用方法:<?phpshowThumbnail($this);?>2、获取文章第一张图片做缩略图functionshowThumbnail($widget){  $attach=$widget->attachments(1)->attachment;  $pattern='/\<img.*?src\=\"(.*?)\"[^>]*>/i';  if(preg_match_all($pattern,$widget->content,$thumbUrl)){    echo$thumbUrl[1][0];  elseif($attach->isImage){    echo$attach->url;  }else{    echo$random;  }}调用方法:<imgsrc="<?phpshowThumbnail($this);?>">3、评论自动排第一functionAutofirst(){$db=Typecho_Db::get();$query=$db->select()->from('table.comments')->where('authorId=?','0')->order('coid',Typecho_Db::SORT_DESC)->limit(100);$result=$db->fetchAll($query);$arrUrl=array();$arrAuthor=array();foreach($resultas$value){if($value["url"]!==null){array_push($arrUrl,$value["url"]);array_push($arrAuthor,$value["author"]);}}$su=array_filter(array_merge(array_unique($arrUrl)));$sa=array_filter(array_merge(array_unique($arrAuthor)));$num=0;for($i=0;$i<count(array_unique($su));$i++){if($su[$i]!==""&&$num<8){$num+=1;$db1=Typecho_Db::get();$query1=$db1->select()->from('table.comments')->where('url=?',$su[$i])->order('coid',Typecho_Db::SORT_DESC)->limit(100);$result1=$db1->fetchAll($query1);$arrAuthor1=array();foreach($result1as$value){array_push($arrAuthor1,$value["author"]);}echo'<divclass="col-lg-3col-md-3item"><ahref="'.$su[$i].'"rel="externalnofollow"class="btnbtn-defaultbtn-blockoverflow"target="_blank">'.$arrAuthor1[0].'</a></div>';}}}调用方法:<?phpAutofirst(100)?>4、随机文章functiongetRandomPosts($limit=10){$db=Typecho_Db::get();$result=$db->fetchAll($db->select()->from('table.contents')->where('status=?','publish')->where('type=?','post')->where('created<=unix_timestamp(now())','post')->limit($limit)->order('RAND()'));if($result){$i=1;foreach($resultas$val){if($i<=3){$var='class="red"';}else{$var='';}$val=Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);$post_title=htmlspecialchars($val['title']);$permalink=$val['permalink'];echo'<li><i'.$var.'>'.$i.'</i><ahref="'.$permalink.'"title="'.$post_title.'"target="_blank">'.$post_title.'</a></li>';$i++;}}}调用方法:<?phpgetRandomPosts('10');?>5、热门文章functiongetHotComments($limit=10){$db=Typecho_Db::get();$result=$db->fetchAll($db->select()->from('table.contents')->where('status=?','publish')->where('type=?','post')->where('created<=unix_timestamp(now())','post')//添加这一句避免未达到时间的文章提前曝光->limit($limit)->order('commentsNum',Typecho_Db::SORT_DESC));if($result){foreach($resultas$val){$val=Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);$post_title=htmlspecialchars($val['title']);$permalink=$val['permalink'];echo'<li><ahref="'.$permalink.'"title="'.$post_title.'"target="_blank">'.$post_title.'</a></li>';}}}调用方法:<?phpgetHotComments('10');?>6、文章阅读量统计functionget_post_view($archive){$cid=$archive->cid;$db=Typecho_Db::get();$prefix=$db->getPrefix();if(!array_key_exists('views',$db->fetchRow($db->select()->from('table.contents')))){$db->query('ALTERTABLE`'.$prefix.'contents`ADD`views`INT(10)DEFAULT0;');echo0;return;}$row=$db->fetchRow($db->select('views')->from('table.contents')->where('cid=?',$cid));if($archive->is('single')){$db->query($db->update('table.contents')->rows(array('views'=>(int)$row['views']+1))->where('cid=?',$cid));}echo$row['views'];}调用方法:<?phpget_post_view($this)?>