Ryan

Handsome主题修改之兼容TeMenu插件
周五有空把Handsome主题更新了,发现作者采纳了博主的意见加入了隐藏特定页面的功能。不过博主感觉功能还是太弱,...
扫描右侧二维码阅读全文
22
2018/07

Handsome主题修改之兼容TeMenu插件

周五有空把Handsome主题更新了,发现作者采纳了博主的意见加入了隐藏特定页面的功能。

不过博主感觉功能还是太弱,所以这周末做了一点微小的工作,让Handsome主题兼容TeMenu插件。

TeMenu插件

TeMenu是Typecho下唯一一款菜单管理插件,体验和Windows下相差不太远。

导航菜单支持添加分类链接、独立页面链接以及自定义链接

导航菜单链接支持自定义链接图标及打开方式

导航菜单支持多级菜单(样式需自行设置)

可添加多个导航菜单以应对不同需求

下载地址

收费插件,请到官网购买下载。
http://lixianhua.com/temenu_plugin_for_typecho.html

兼容方法

1、到usr/themes/handsome/libs目录下新增Menu.php
Menu.php的内容如下所示

<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
 * Menu.php
 * Author     : Ryan
 * Date       : 2018/07/21
 * Version    : 1.0.0
 * Description: 使用PHP方法输出菜单
 */
class Menu {
    /**
     * 菜单初始化
     * @param string $slug
     * @return void
     */
    public function parse($slug) {
        $html = '';
        $all = Typecho_Plugin::export();
        if (array_key_exists('TeMenu', $all['activated'])) {
            $navigation = json_decode(Typecho_Widget::widget('Widget_Options')->plugin('TeMenu')->navigation, true);
            if(!array_key_exists($slug, $navigation)){
                $html = '<li><a href="' . str_replace('login.php','',Typecho_Widget::widget('Widget_Options')->loginUrl) .'extending.php?panel=TeMenu%2Fpanel.php"><i class="fontello fontello-menu"></i><span>请到后台新增菜单</span></a></li>';
            } else {
                $menu = $navigation[$slug]['menu'];
                $html = self::parseMenu($menu);
            }
        } else {
            $html = '';
            $html.= self::title('导航');
            $hideHomeItem = false;
            if(!empty(Typecho_Widget::widget('Widget_Options')->asideItems)){
                $json = '['.Typecho_Widget::widget('Widget_Options')->asideItems.']';
                $asideItems = json_decode($json);
                $asideItemsOutput = "";
                foreach ($asideItems as $asideItem){
                    $itemName = $asideItem->name;
                    @$itemStatus = $asideItem->status;
                    @$itemLink = $asideItem->link;
                    @$itemClass = $asideItem->class;
                    @$itemTarget = $asideItem->target;
                    if ($itemName === 'home' && strtoupper($itemStatus) === 'HIDE'){
                        $hideHomeItem = true;
                        continue;//本次循环结束,不再执行下面内容
                    }
                    if (@$itemTarget){
                        $linkStatus = 'target="'.$itemTarget.'"';
                    }else{
                        $linkStatus = 'target="_blank"';
                    }
                    $asideItemsOutput .= '<li> <a '.$linkStatus.' href="'.$itemLink.'" class ="auto"><i class="'.$itemClass.' icon text-md"></i><span>'._mt($itemName).'</span></a></li>';
                }
            }
            if (!$hideHomeItem) { //首页
                $html .= '<!--主页--><li><a href="' . Typecho_Widget::widget('Widget_Options')->rootUrl . '/" class="auto"><i class="iconfont icon-zhuye icon text-md"></i><span>' . _mt("首页") . '</span></a></li><!-- /主页 -->';
            }
            $html .= @$asideItemsOutput;
            if (@!in_array('component',Typecho_Widget::widget('Widget_Options')->asideSetting)) {
                $html .= self::line(); //分割线
                $html .= self::title("组成");
                $html .= self::categories(); //分类
                $html .= self::pages();
                $html .= self::friends();
            }
        }
        echo $html;
    }
    /**
     * 生成菜单 HTML
     * @param string $menu
     * @return string
     */
    public function parseMenu($menu, $depth = 1) {
        $html = '';
        $archive = Typecho_Widget::widget('Widget_Archive');
        foreach($menu as $k=>$v){
            if ($v['type']=='category'){
                $category = Typecho_Widget::widget('Widget_Metas_Category_List')->getCategory($v['id']);
                $v['url'] = $category['permalink'];
                $v['slug'] = $category['slug'];
                $v['current'] = $archive->is('category',$category['slug']);
            } elseif ($v['type']=='page'){
                $page = Typecho_Widget::widget('Menu_Page_List')->getPage($v['id']);
                $v['url'] = $page['permalink'];
                $v['slug'] = $page['slug'];
                $v['current'] = $archive->is('page',$page['slug']);
            } else {
                $url = $archive->request->getRequestUrl();
                $v['url'] = $v['id'];
                $v['current'] = $url === $v['url'];
            }
            $v['target'] = isset($v['target']) && $v['target'] ? 'target="_blank"' : '';
            $v['class'] = "";
            switch($v['icon']) {
                case "title":
                    $html .= self::title($v['name']);
                    break;
                case "friends":
                    $html .= self::friends($v['name']);
                    break;
                case "pages":
                    $html .= self::pages($v['name']);
                    break;
                case "categories":
                    $html .= self::categories($v['name']);
                    break;
                case "line":
                    $html .= self::line();
                    break;
                default:
                    if ($depth == 1) { //一级菜单处理
                        $v['icon'] = (isset($v['icon']) && $v['icon'] != '') ? '<i class="'.$v['icon'].' icon text-md"></i>' : '<i class="glyphicon glyphicon-file icon text-md"></i>'; //字体图标
                        $v['caret'] = ((isset($v['children'])) ? '<span class="pull-right text-muted"><i class="fontello icon-fw fontello-angle-right text"></i><i class="fontello icon-fw fontello-angle-down text-active"></i></span>' : ''); //大于号
                        $v['url'] = (isset($v['children'])) ? '#':$v['url'];
                    } else {
                        $v['icon'] = '';
                    }
                    $html .=  '<li class="'. $v['class'] .'">';
                    $html .= str_replace(array('{url}','{name}','{class}','{icon}','{target}', '{caret}'),
                        array($v['url'],$v['name'],"auto",$v['icon'],$v['target'],$v['caret']),'<a class="{class}" href="{url}" {target}>{caret}{icon}<span>{name}</span></a>');
                    if(isset($v['children'])){
                        $html .= '<ul class="nav nav-sub dk">';
                        $html .= self::parseMenu($v['children'], $depth+1);
                        $html .= '</ul>';
                    } 
                    break;
            }
            $html .= '</li>';
        }
        return $html;
    }
    public function title($title = "菜单") {
        return '<li class="hidden-folded padder m-t m-b-sm text-muted text-xs"><span>' . _mt($title) .'</span></li>';
    }
    public function line() {
        return '<li class="line dk"></li>';
    }
    public function pages($title = "页面") {
        $html = '';
        $html .= '<li><a class="auto"><span class="pull-right text-muted"><i class="fontello icon-fw fontello-angle-right text"></i><i class="fontello icon-fw fontello-angle-down text-active"></i></span><i class="glyphicon glyphicon-file"></i><span>' . _mt($title) . '</span></a><ul class="nav nav-sub dk"><li class="nav-sub-header"><a data-no-instant><span>' . _mt($title) . '</span></a></li>';
        Typecho_Widget::widget('Widget_Contents_Page_List')->to($pages);
        while($pages->next()) {
            if ($pages->fields->navbar == "hide") continue; //隐藏指定页面
            $html .= '<li><a href="' . $pages->permalink . '"><span>' . $pages->title . '</span></a></li>';
        }
        $html .= '</ul></li>';
        return $html;
    }
    public function categories($title = '分类') {
        $html = '';
        $html .= '<!--分类category--><li><a class="auto"><span class="pull-right text-muted"><i class="fontello icon-fw fontello-angle-right text"></i><i class="fontello icon-fw fontello-angle-down text-active"></i></span><i class="glyphicon glyphicon-th"></i><span>' . _mt($title) . '</span></a><ul class="nav nav-sub dk"><li class="nav-sub-header"><a data-no-instant><span>' . _mt($title) . '</span></a></li>';
        //循环输出分类
        Typecho_Widget::widget('Widget_Metas_Category_List')->to($categories);
        while($categories->next()){
            if ($categories->levels === 0){//父亲分类

                $children = $categories->getAllChildren($categories->mid);//获取当前父分类所有子分类
                //print_r($children);
                //var_dump(empty($children));
                if (!empty($children)){//子分类不为空
                    $html .= '<li><a class="auto"><span class="pull-right text-muted">
                    <i class="fontello icon-fw fontello-angle-right text"></i>
                    <i class="fontello icon-fw fontello-angle-down text-active"></i>
                  </span><span>'.$categories->name.'</span></a>';
                    //循环输出子分类
                    $childCategoryHtml = '<ul class="nav nav-sub dk child-nav">';
                    $childCategoryHtml .= '<li><a href="'.$categories->permalink.'"><b class="badge pull-right">'.$categories->count.'</b><span>'.$categories->name.'</span></a></li>';
                    foreach ($children as $mid){
                        $child = $categories->getCategory($mid);
                        $childCategoryHtml .= '<li><a href="'.$child['permalink'].'"><b class="badge pull-right">'.$child['count'].'</b><span>'.$child['name'].'</span></a></li>';
                    }
                    $childCategoryHtml .= '</ul>';

                    $html .= $childCategoryHtml;
                    $html .= "</li>";
                }else{//没有子分类
                    $html .= '<li><a href="'.$categories->permalink.'"><b class="badge pull-right">'.$categories->count.'</b><span>'.$categories->name.'</span></a></li>';
                }
            }
        }
        $html .= '</ul></li>';
        return $html;
    }
    public function friends($title = "友链") {
        return '<li><a class="auto"><span class="pull-right text-muted"><i class="fontello icon-fw fontello-angle-right text"></i><i class="fontello icon-fw fontello-angle-down text-active"></i></span><i class="iconfont icon-links"></i><span>' . _mt($title) . '</span></a><ul class="nav nav-sub dk"><li class="nav-sub-header"><a data-no-instant><span>' . _mt($title) . '</span></a></li>' . Links_Plugin::output_str("<li><a href=\"{url}\" target=\"_blank\" title=\"{title}\"><span>{name}</span></a></li>", 0, "ten") . '</ul></li>';
    }
}
class Menu_Page_List extends Widget_Abstract_Contents
{
     /**
     * 所有页面哈希表 
     * 
     * @var array
     * @access private
     */
    private $_map = array();
    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
        $select = $this->select()->where('table.contents.type = ?', 'page')
        ->where('table.contents.created < ?', $this->options->gmtTime)
        ->order('table.contents.order', Typecho_Db::SORT_ASC);

        //去掉自定义首页
        $frontPage = explode(':', $this->options->frontPage);
        if (2 == count($frontPage) && 'page' == $frontPage[0]) {
            $select->where('table.contents.cid <> ?', $frontPage[1]);
        }

        $rs = $this->db->fetchAll($select, array($this, 'push'));
        foreach($rs as $v){
            $this->_map[$v['cid']] = $v;
        }
    }
    /**
     * 获取单个页面
     * 
     * @param integer $cid 
     * @access public
     * @return mixed
     */
    public function getPage($cid)
    {
        return isset($this->_map[$cid]) ? $this->_map[$cid] : NULL;
    }
}

2.然后修改functions.php引入菜单组件

require_once("libs/Content.php");
require_once("libs/Menu.php"); //新增这一行

3.修改usr/themes/handsome/component/aside.php
把导航代码(两个<!-- nav -->中间的代码)替换为

<nav ui-nav class="navi clearfix">
  <ul class="nav">
      <?php Menu::parse('aside'); ?>
  </ul>
</nav>

4.到这里,你的左侧导航菜单已经由TeMenu插件接管了。

Tips

1.如果没有TeMenu插件被禁用,菜单和以前一样没有任何变化
2.没有设置菜单左侧导航栏会提示你到后台新增菜单,点击就能进入菜单管理页面
3.菜单标志为aside
please_setup_menu.png
3.特殊菜单(只能是一级菜单),没有处理非一级的,(你可以试试放到二级)

特殊菜单
全站友链:把图标设置样式 friends,标题写你想显示的
分类:把图标设置样式 categories,标题写你想显示的
导航、组成:把图标设置样式 title,标题写你想显示的
页面:把图标设置样式 pages,标题写你想显示的
分割线:把图标设置样式 line

00:00
加载中……请稍等……

搬瓦工年付$187机房套餐补货了,电信联通优化,512M内存/500G流量/1G带宽,建站稳定,优惠码:BWH1ZBPVK,【点击购买】!
搬瓦工年付$28CN2高速线路,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快,建站稳定,优惠码同上,【点击购买】!
Last modification:October 7th, 2018 at 09:08 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment

5 comments

  1. 女生游戏

    能将代码做成插件就更好了

  2. 猫腻

    看看

    1. Ryan
      @猫腻

      回复邮件测试

  3. 芭比

    看到域名和标题裤子都脱了,一进来就给我看代码

    1. Ryan
      @芭比

      哈哈