来源:城市@后 酷勤网收集 2008-10-06
摘要
如果你有20个不同的标签,标签云不失为一种很好的选择,但是如果你有100个或是更多数量的标签,那么你的标签云就会很杂乱、很难阅读!那么是不是标签数量很大时我们就无法完美地将其在首页显示呢?我们的解决办法就是——用下拉菜单来显示标签!
标签(Tags)对于任何一个博客来说都是非常重要的:正如你所知道的那样,它允许用户显示一个主题下所有相关日志的列表。在多数情况下标签(Tags)是可以用一个标签云(Tags Cloud)来显示的。
如果你有20个不同的标签,标签云不失为一种很好的选择,但是如果你有100个或是更多数量的标签,那么你的标签云就会很杂乱、很难阅读!也更没有谁会去点击你的标签云! 这也许是为什么近来许多博客都撤掉了原有的标签云功能,或者是建立一个单独的页面来放置这些数量庞大的标签。
那么是不是标签数量很大时我们就无法完美地将其在首页显示呢?我们的解决办法就是——用下拉菜单来显示标签!

首先,我们必须创造一个php函数(a php function)。在你的主题文件夹中找到functions.php这个文件,在其中添加如下代码(小心PHP的开放/关闭标记!)
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 86 87 |
<?php function dropdown_tag_cloud( $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '' ); $args = wp_parse_args( $args, $defaults ); $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags if ( empty($tags) ) return; $return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args if ( is_wp_error( $return ) ) return false; else echo apply_filters( 'dropdown_tag_cloud', $return, $args ); } function dropdown_generate_tag_cloud( $tags, $args = '' ) { global $wp_rewrite; $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC' ); $args = wp_parse_args( $args, $defaults ); extract($args); if ( !$tags ) return; $counts = $tag_links = array(); foreach ( (array) $tags as $tag ) { $counts[$tag->name] = $tag->count; $tag_links[$tag->name] = get_tag_link( $tag->term_id ); if ( is_wp_error( $tag_links[$tag->name] ) ) return $tag_links[$tag->name]; $tag_ids[$tag->name] = $tag->term_id; } $min_count = min($counts); $spread = max($counts) - $min_count; if ( $spread <= 0 ) $spread = 1; $font_spread = $largest - $smallest; if ( $font_spread <= 0 ) $font_spread = 1; $font_step = $font_spread / $spread; // SQL cannot save you; this is a second (potentially different) sort on a subset of data. if ( 'name' == $orderby ) uksort($counts, 'strnatcasecmp'); else asort($counts); if ( 'DESC' == $order ) $counts = array_reverse( $counts, true ); $a = array(); $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : ''; foreach ( $counts as $tag => $count ) { $tag_id = $tag_ids[$tag]; $tag_link = clean_url($tag_links[$tag]); $tag = str_replace(' ', ' ', wp_specialchars( $tag )); $a[] = "\t<option value='$tag_link'>$tag ($count)</option>"; } switch ( $format ) : case 'array' : $return =& $a; break; case 'list' : $return = "<ul class='wp-tag-cloud'>\n\t<li>"; $return .= join("</li>\n\t<li>", $a); $return .= "</li>\n</ul>\n"; break; default : $return = join("\n", $a); break; endswitch; return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args ); } ?> |
一旦你创建成功了这个函数,你就可以在你主题的任何地方调用它,并显示你想要的标签列表。大多数情况下我们都把它放在侧边(sidebar.php)栏里。
OK!粘贴以下代码到侧边栏
1 2 3 4 |
<select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> <option value="#">Liste Tags</option> <?php dropdown_tag_cloud('number=0&order=asc'); ?> </select> |
现在,刷新你的博客你将会看到一个非常漂亮的标签下拉菜单列表

