Home » Wordpress 技巧

如何为你的WordPress博客建立标签下拉菜单

有的朋友喜欢在博客的首页建立一个文章标签区域,这样既方便读者的阅读,对博客的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(‘ ‘, ‘&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 );
}
?>

上面代码对标签下拉菜单进行指示,添加完之后你把下面的代码放到主题的某个位置,这样你的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% [?]

Share/Bookmark this!

2 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on December 28, 2008 at 3:07 am and filed under Wordpress 技巧 category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

最新文章

最新评论

Tag Cloud

301 Redirect 301重定向 2010 about All in one seo pack AutosCraze blog CSS Darren feed FreshMag Global Translator Google GreenFun HTML Insider name.com Problogger robots.txt文件 RSS Feed seo Wordpress Wordpress 主题 Wordpress主题 代码 优惠码 关于 博客 博客建设 域名 工具 建设 技巧 提交 插件 文章 杂志型主题 标签 注册 绿色 网赚 评论 选择 链接 页面