主要实现分类页面、文章页面,自动识别当前分类并添加css样式。首页会被识别为默认分类。
导航代码:

<?php $this->widget('Widget_Metas_Category_List')->to($categories); ?>
<?php while($categories->next()): ?>
<?php if ($categories->levels === 0): ?>
<a<?php if($this->is('category', $categories->slug)||($categories->slug == topcategory($this->category))): ?> class="current"<?php endif; ?> href="<?php $categories->permalink(); ?>"><?php $categories->name(); ?></a>
<?php endif; ?>
<?php endwhile; ?>

获取文章所属分类的父分类函数:

function topcategory($slug) {
    $db = Typecho_Db::get(); 
    $prefix = $db->getPrefix(); 
    $rs = $db->fetchRow($db->select()->from($prefix.'metas')->where('slug = ?', $slug)->limit(1));

    if($rs['parent']==0){
        return $rs['slug'];
    }
    else {
        $rs2 = $db->fetchRow($db->select()->from($prefix.'metas')->where('mid = ?', $rs['parent'])->limit(1));
        return $rs2['slug'];
    }
}