如何为你的WordPress博客建立标签下拉菜单
December 28, 2008 # 3:07 am # Wordpress 技巧 # 2 Comments有的朋友喜欢在博客的首页建立一个文章标签区域,这样既方便读者的阅读,对博客的SEO也有一定的帮助。不过随着文章标签的的增加,博客的文章标签这一块可能就会显地越来越拥挤,进而影响整个博客的美观性。你可以安装Flash WordPress 标签云插件WP Cumulus来解决这一问题。不过据我所知,该插件目前并不支持中文标签的显示。今天我为大家介绍一个为你的WordPress博客建立标签下拉菜单的方法,这样就可以解决上面提到的问题了。
首先,复制下面的代码到主题的functions.php文件里面
<?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 );
}
?>
上面代码对标签下拉菜单进行指示,添加完之后你把下面的代码放到主题的某个位置,这样你的WordPress博客建立标签下拉菜单就可以显示了。
<select name=”tag-dropdown” onchange=”document.location.href=this.options[this.selectedIndex].value;”>
<option value=”#”>文章标签下拉菜单</option>
<?php dropdown_tag_cloud(‘number=0&order=asc’); ?>
</select>
代码中的“文章标签下拉菜单”可以随意修改。效果预览可以看看博客的侧边栏
Popularity: 2% [?]
Subscribe RSS
Comment RSS







技术文章,支持
我会继续努力的,希望会有更多的人支持!