WordPress制作一个首字母排序的标签页面模板
以前做过一个wordpress实现彩色标签云列表单独页面,
现在我又自己新做了一个零食推荐网站:爱吃零食
做完后总觉得不是很好看,我看了别人很多的标签存档页面,发现有很多是以首字母排序来做的。
这个功能很高大上,马上我就想自己也做一个。
做完后的效果大家看看:吃货热搜
下面来谈谈制作方法:
新建页面模板不多说了,上面的那篇文章有介绍。
下面贴出主要的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | function specs_getfirstchar($s0){ $fchar = ord($s0{0}); if($fchar >= ord("A") and $fchar < = ord("z") )return strtoupper($s0{0}); $s1 = iconv("UTF-8","gb2312", $s0); $s2 = iconv("gb2312","UTF-8", $s1); if($s2 == $s0){$s = $s1;}else{$s = $s0;} $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if($asc >= -20319 and $asc < = -20284) return "A"; if($asc >= -20283 and $asc < = -19776) return "B"; if($asc >= -19775 and $asc < = -19219) return "C"; if($asc >= -19218 and $asc < = -18711) return "D"; if($asc >= -18710 and $asc < = -18527) return "E"; if($asc >= -18526 and $asc < = -18240) return "F"; if($asc >= -18239 and $asc < = -17923) return "G"; if($asc >= -17922 and $asc < = -17418) return "H"; if($asc >= -17417 and $asc < = -16475) return "J"; if($asc >= -16474 and $asc < = -16213) return "K"; if($asc >= -16212 and $asc < = -15641) return "L"; if($asc >= -15640 and $asc < = -15166) return "M"; if($asc >= -15165 and $asc < = -14923) return "N"; if($asc >= -14922 and $asc < = -14915) return "O"; if($asc >= -14914 and $asc < = -14631) return "P"; if($asc >= -14630 and $asc < = -14150) return "Q"; if($asc >= -14149 and $asc < = -14091) return "R"; if($asc >= -14090 and $asc < = -13319) return "S"; if($asc >= -13318 and $asc < = -12839) return "T"; if($asc >= -12838 and $asc < = -12557) return "W"; if($asc >= -12556 and $asc < = -11848) return "X"; if($asc >= -11847 and $asc < = -11056) return "Y"; if($asc >= -11055 and $asc < = -10247) return "Z"; return null; } function specs_pinyin($zh){ $ret = ""; $s1 = iconv("UTF-8","gb2312", $zh); $s2 = iconv("gb2312","UTF-8", $s1); if($s2 == $zh){$zh = $s1;} $s1 = substr($zh,$i,1); $p = ord($s1); if($p > 160){ $s2 = substr($zh,$i++,2); $ret .= specs_getfirstchar($s2); }else{ $ret .= $s1; } return strtoupper($ret); } function specs_show_tags() { $categories = get_terms( 'post_tag', array( 'orderby' => 'count', 'order' => 'desc', 'hide_empty' => 1 ) ); foreach($categories as $v){ for($i = 65; $i < = 90; $i++){ if(specs_pinyin($v->name) == chr($i)){ $r[chr($i)][] = $v; } } } ksort($r); $output = "<ul class='tag-letter'>"; for($i=65;$i< =90;$i++){ $tagi = $r[chr($i)]; if(is_array($tagi)){ $output .= "<li><a href='#".chr($i)."'>".chr($i)."</a>"; }else{ $output .= "<li><span>".chr($i)."</span></li>"; } } $output .= "</ul>"; $output .= "<ul class='tag-list'>"; for($i=65;$i< =90;$i++){ $tagi = $r[chr($i)]; if(is_array($tagi)){ $output .= "<li id='".chr($i)."'><h4 class='tag-name'>".chr($i)."</h4>"; foreach($tagi as $tag){ $color = dechex(rand(0,16777215)); $output .= "<a href='".get_tag_link($tag->term_id)."' style='color:#{$color}'>".$tag->name."(".$tag->count.")</a>"; } } } $output .= "</ul>"; echo $output; } |
然后调用就直接:
1 | specs_show_tags(); |
下面是爱吃零食的标签存档的css样式:
1 2 3 4 5 6 7 8 9 | .tag-letter li{float: left;margin-right: 5px;} .tag-letter a,.tag-letter span{display: block;text-decoration: none;background: #EA84BA; color: #f8f8f8; margin: 1px; padding: 6px 10px;font-weight: bolder;} .tag-letter span{color: #ccc;} .tag-letter a:hover,.tag-letter a.current {background: #ff6000;} .tag-name{ font-weight: bold;margin: 10px 0;padding-left: 10px;border-bottom: 1px solid #f2f2f2;color: #666;font-size: 18px;line-height: 28px;} .tag-list{float:left; line-height:30px; margin:25px 0 0 26px; font-size:14px; color:#000;line-height: 200%;margin: 2px;} .tag-list li{width: 100%;float: left;padding: 5px;} .tag-list a{padding: 2px 8px;font-weight: bold;} .tag-list a:hover{color: #EA84BA;} |
大家看看,是不是挺好看的,等有时间,我把明凯博客的标签存档页面也改成这样子的。
javascript url post 特殊字符转义 + & # 的解决办法 获取sqlserver数据库中所有库、表、字段名的方法汇总