侧栏文章增加排列序号
<?php $i = 1; ?>
<?php while($post->next()): ?>
<li><span class="label label-<?php echo $i; ?>"><?php echo $i; ?></span><a href="<?php $post->permalink(); ?>"><?php $post->title(); ?></a></li>
<?php $i++; ?>
<?php endwhile; ?>
{/collapse-item}
{collapse-item label="各种判断"}
<?php if($this->is('index')):?>
首页
<?php if($this->is('category')):?>
分类
<?php if($this->is('category', 'category1')):?>
独立分类
<?php if($this->category == "help"): ?>
首页判断文章所在分类
<?php if($this->is('page')):?>
页面
<?php if($this->is('page', 'page1')):?>
独立页面
<?php if($this->is('post')):?>
内容页
<?php if ($pages->slug != 'page1'): ?>
导航不显示某页面
<?php if (($pages->slug != 'page1') && ($pages->slug != 'page2')): ?>
导航不显示某两个页面
<?php if ($category->slug != 'category1'): ?>
导航不显示某分类
<?php if (($category->slug != 'category1') && ($pages->slug != 'category2')): ?>
导航不显示两个分类
<?php if($this->category != "category1"): ?>
首页不显示某分类
<?php if (($this->_currentPage == 1) && ($this->sequence == 1)): ?>
首页第一篇文章
<?php elseif (array_key_exists('字段值',unserialize($this->___fields()))): ?>
自定义字段
<?php if($this->user->hasLogin()): ?>
是否登录
<?php endif; ?>
{/collapse-item}
自定义字段:
<?php if (array_key_exists('image',unserialize($this->___fields()))): ?>
<span class="icon-image"></span>
<?php elseif (array_key_exists('music',unserialize($this->___fields()))): ?>
<span class="icon-music"></span>
<?php elseif (array_key_exists('play',unserialize($this->___fields()))): ?>
<span class="icon-play"></span>
<?php endif; ?>
首页不显示某分类
<?php while($this->next()): ?>
<?php if($this->category != "cateslug"): ?>
//正常输出循环
<?php endif; ?>
<?php endwhile; ?>
调用相关文章
<?php $this->related(5)->to($relatedPosts); ?>
<ul>
<?php while ($relatedPosts->next()): ?>
<li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
<?php endwhile; ?>
</ul>
调用指定分类文章
<?php
$this->widget('Widget_Archive@index', 'pageSize=6&type=category', 'mid=3')
->parse('<li><a href="{permalink}">{title}</a></li>');
?>
利用Typecho函数调用自定义分类内容显示方法
在模板的functions.php文件中,加入下面的代码
function themeInit($archive) {
if ($archive->is('category', 'jobs')) {
$archive->parameter->pageSize = 8; // 自定义条数
}
}
同时设定两种不同分类列表下显示不同文章输出数量的实现办法
function themeInit($archive) {
if ($archive->is('category', 'jobs')) {
$archive->parameter->pageSize = 8; // 自定义条数
}elseif($archive->is('category', 'news')){
$archive->parameter->pageSize = 12; // 自定义条数
}
}
Typecho 程序如何调用循环页面、分类、标签代码方法
<!--循环显示页面-->
<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
<?php while($pages->next()): ?>
<span class="nav-item<?php if($this->is('page', $pages->slug)): ?> nav-item-current<?php endif; ?>">
<a href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>">
<span><?php $pages->title(); ?></span>
</a>
</span>
<?php endwhile; ?>
<!--结束显示页面-->
<!--循环所有分类-->
<?php $this->widget('Widget_Metas_Category_List')->to($category); ?>
<?php while ($category->next()): ?>
<span class="nav-item<?php if($this->is('category', $category->slug)): ?> nav-item-current<?php endif; ?>">
<a href="<?php $category->permalink(); ?>" title="<?php $category->name(); ?>">
<span><?php $category->name(); ?></span>
</a>
</span>
<?php endwhile; ?>
<!--结束显示分类-->
<!--循环显示标签 其中limit的5为显示数量-->
<?php $this->widget('Widget_Metas_Tag_Cloud', array('sort' => 'count', 'ignoreZeroCount' => true, 'desc' => true, 'limit' => 5))->to($tags); ?>
<?php while($tags->next()): ?>
<span class="nav-item<?php if($this->is('tag', $tags->slug)): ?> nav-item-current<?php endif; ?>">
<a href="<?php $tags->permalink(); ?>" title="<?php $tags->name(); ?>">
<span><?php $tags->name(); ?></span>
</a>
</span>
<?php endwhile; ?>
<!--结束显示标签-->
不同分类调用不同的模板页面
<?php
if($this->is('category','default')){
$this->need('default.php');
}elseif($this->is('category','technology')){
$this->need('technology.php');
}else{
$this->need('other.php');
}
?>
或者
<?php
$slugArray = array('default','technology');
foreach($slugArray as $slug){
if($this->is('category',$slug)){
$this->need('default.php');
}else{
$this->need('other.php');
}
}
?>
Typecho最新24小时内文章加上NEW标签方法
/**
* 时间函数
*
*/
function timeZone($from){
$now = new Typecho_Date(Typecho_Date::gmtTime());
return $now->timeStamp - $from < 246060 ? true : false;
}
<?php if(timeZone($this->date->timeStamp)) echo ' new'; ?>
最新文章调用
<?php $this->widget('Widget_Contents_Post_Recent')->to($post); ?>
<?php while($post->next()): ?>
<a href=”<?php $post->permalink(); ?>” title=”<?php $post->title(); ?>”>
<?php $post->title(25, '…'); ?></a>
<?php endwhile; ?>
热门文章调用
function getHotComments($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($result as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';
}
}
}
顺便逛逛居然看到了我以前的域名maxwuhan,博客圈真小,哈哈