2015年10月11日 星期日

將Blogger的標籤改成樹狀階層式

使用[分類名稱1][分類名稱2]標籤名稱的格式來定義標籤即可
1.打開【版面配置】,【新增小工具】加入【標籤】這個小工具, 設定為用按字母排序。

2.打開【範本】選擇【編輯HTML】,找像下列的字串
<b:widget id="Label1" locked="false" title="標籤" type="Label">
一直到
一直到
一直到
</b:widget>

3.將上述範圍內的內容改為:
<b:widget id='Label3' locked='false' title='文章分類' type='Label'>
<b:includable id='main'>
  <b:if cond='data:title'>
    <h2><data:title/></h2>
  </b:if>
  <div expr:class='&quot;widget-content &quot; + data:display + &quot;-label-widget-content&quot;'>
<script>
var label_tree= new Object();

function tn(tag)
{
    return tag.substring(1,tag.length-1);
}

function build_node(tree,idx,cnt)
{
    if(!tree[idx[0]]){
        tree[idx[0]]=new Object();
        tree[idx[0]]['_cnt']=0;
        tree[idx[0]]['_item']=new Array();
    }
    tree[idx[0]]['_cnt'] = parseInt(tree[idx[0]]['_cnt']) + parseInt(cnt);      
    if(idx.length==1){
        return tree[idx[0]];      
    }
    return build_node(tree[idx[0]],idx.slice(1),cnt);
}
function add_label(_name,_cnt,_url,_dir)
{
    var e1 = new RegExp("\\[(.+?)\\]","g");
    var e = new RegExp("\\[(.+)\\]","g");
    var m = _name.match(e);
    var tag = _name;
   
    var obj=new Object();
    obj['tag']=_name;
    obj['cnt']=_cnt;
    obj['dir']=_dir;
    obj['url']=_url;
    if(m){
        obj['tag']= _name.substring(m[0].length,_name.length);              
        var m1 = _name.match(e1);
        if(m1){
            node=build_node(label_tree,m1,_cnt);
            if(node){          
                node['_item'].push(obj);
            }
        }      
    }
    else{
        if(!label_tree['_item']){
            label_tree['_item']=new Array();
        }
        label_tree['_item'].push(obj);
    }
}
function menu_exp(id)
{
        var ul=document.getElementById("ul_"+id);
        var li=document.getElementById("li_"+id);
        if (ul.style.display=="none"){
            ul.style.display="block"
            li.style.backgroundImage="url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj57QQ1QYisKlsJcRabX_llGi1-0ISoFI8kLVYKUxngRykysrTYs_C2OYULGOMfd6QEV9Ln7aoXhzmxwAgY3aGb_Aj_J4989we8pJA9tUY-3fKj9xU4rIXDfBUWc6Oca27oTLaFl6lRa10/)"
        }
        else {
            ul.style.display="none"
            li.style.backgroundImage="url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsePcXLAHwDkI45kMIk7UQ4Fsf0Fp6kbm_w1LgvZMI2DmzBu4gR87QjTg2NZczmc7oad3PZOPRZjeoIikdgGrbHpqMfFdpzlauPOeJUHOBlCwsdvTa4QH7F0gAYs2JAgKixgJQJfbhWfk/)"
        }      
}
function draw_obj(obj)
{
    document.write("&lt;li style='padding-left: 0px&gt;'");
    document.write("&lt;img src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYbLtB8BPZUPQ-ZbY0U1EhsVk8kYUoh98CTW37JW7XeM5GrH8_NJd8ewucXdMXw7dL7qJdeMmAj6AZCFZ4VAUmZK9gXFFYNBq4cI5AiEubw6uHLw5KSwwu0Hl8JOLRSOtfZdMgAcqwJIU/s1600/icon-yellow-bullet.png' /&gt;&lt;a dir='"+obj['dir']+"' href='"+obj['url']+"'&gt;"+obj['tag']+"&lt;/a&gt;");
    document.write("&lt;span dir='"+obj['dir']+"'&gt;("+obj['cnt']+")&lt;/span&gt;");
    document.write("&lt;/li&gt;\n");
}
function draw_tree(tree)
{
    for(var tag in tree){
        if(tag!='_item'&amp;&amp; tag!='_cnt'){
            var rid = Math.random();
            document.write("&lt;li class=submenu id=li_"+rid+" onclick=menu_exp('"+rid+"'); style='font-weight:bold;padding-left: 20px;background: url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsePcXLAHwDkI45kMIk7UQ4Fsf0Fp6kbm_w1LgvZMI2DmzBu4gR87QjTg2NZczmc7oad3PZOPRZjeoIikdgGrbHpqMfFdpzlauPOeJUHOBlCwsdvTa4QH7F0gAYs2JAgKixgJQJfbhWfk/) no-repeat left 0px;cursor: pointer'&gt;");
            document.write(tn(tag));
            document.write("("+tree[tag]['_cnt']+")");
            document.write("&lt;/li&gt;");
            document.write("&lt;ul id=ul_"+rid+" style='display:none;padding-left: 20px'&gt;");
            draw_tree(tree[tag]);
            document.write("&lt;/ul&gt;\n");
        }
    }
    if(tree['_item']){
        for(var i=0;i &lt; tree['_item'].length;i++){
            draw_obj(tree['_item'][i]);
        }
    }
       
}
</script>
<b:loop values='data:labels' var='label'>
<script>
    add_label('<data:label.name/>','<data:label.count/>','<data:label.url/>','<data:blog.languageDirection/>');
</script>
</b:loop>
      <ul>
      <script>
          draw_tree(label_tree);
      </script>
      </ul>
    <b:include name='quickedit'/>
  </div>
</b:includable>
</b:widget>
4.按下【儲存範本】就完成了
重點是在設定標籤名稱時,在【輸入新的標籤】時,一定要使用以下的格式
[分類名稱1][分類名稱2]標籤名稱

沒有留言:

張貼留言