Top Down Contact Closed
Home > hacks > 用下拉菜单显示你的WordPress Tags

用下拉菜单显示你的WordPress Tags

2008/10/02 Add 9 comments |

      标签(Tags)对于任何一个博客来说都是非常重要的:正如你所知道的那样,它允许用户显示一个主题下所有相关日志的列表。在多数情况下标签(Tags)是可以用一个标签云(Tags Cloud)来显示的。

      如果你有20个不同的标签,标签云不失为一种很好的选择,但是如果你有100个或是更多数量的标签,那么你的标签云就会很杂乱、很难阅读!也更没有谁会去点击你的标签云! :mrgreen: 这也许是为什么近来许多博客都撤掉了原有的标签云功能,或者是建立一个单独的页面来放置这些数量庞大的标签。

      那么是不是标签数量很大时我们就无法完美地将其在首页显示呢?我们的解决办法就是——用下拉菜单来显示标签:idea:

首先,我们必须创造一个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(' ', '&nbsp;', 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="#">List Tags</option>
	<?php dropdown_tag_cloud('number=0&order=asc'); ?>
</select>

现在,刷新你的博客你将会看到一个非常漂亮的标签下拉菜单列表 :smile:

Ad spot

声明:城市@后版权所有,未经授权请勿转载本博客日志到任何博客或论坛!

如果你喜欢我的文章,欢迎订阅。Google Reader | 鲜果 | 抓虾 | 九点 | QQ邮箱 | 有道 | 更多

Posted by: Gil | Categories: hacks | Tags: , , , , , | Read: 1,794 views
  1. 10月 4th, 2008 at 13:02 | #1

    这方法不错

    尤其是标签多了难管理的时候

  2. 10月 6th, 2008 at 10:59 | #2

    如果能按标签的热门程度排列标签就好了

  3. 10月 8th, 2008 at 16:03 | #3

    不错的东西,不过我只调用了一部分

  4. 10月 8th, 2008 at 17:37 | #4

    有时间再细看~先收藏~

  5. 10月 21st, 2008 at 16:52 | #5

    @死的蚊:

    死的蚊 :

    如果能按标签的热门程度排列标签就好了

    这个实现很简单,只需要在调用时设置相应的参数即可,你可以参考《菜鸟进阶:浅谈WordPress内置标签云的使用》这篇文章中提到的相关参数进行设定 :cool:

  6. 5月 15th, 2009 at 12:00 | #6

    限制标签云的显示数量就行了

  7. 2月 28th, 2010 at 23:58 | #7

    为什么我照你说的做了,下拉菜单的调用我把它写在边栏的txt新建模块中了,下拉菜单是出来了,可是不能调用到任何tag...