主要实现分类页面、文章页面,自动识别当前分类并添加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'];
    }
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。