我现在可以用下面的代码工作了,不确定这是不是最好的方法,但效果很好
/**
* Tag LIST widget class
*/
class WP_Widget_Tag_List extends WP_Widget
{
function __construct()
{
$widget_ops = array(\'description\' => __("Your most used tags in List format"));
parent::__construct(\'tag_list\', __(\'Tag List\'), $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$current_taxonomy = $this->_get_current_taxonomy($instance);
if (!empty($instance[\'title\'])) {
$title = $instance[\'title\'];
} else {
if (\'post_tag\' == $current_taxonomy) {
$title = __(\'Tags\');
} else {
$tax = get_taxonomy($current_taxonomy);
$title = $tax->labels->name;
}
}
$title = apply_filters(\'widget_title\', $title, $instance, $this->id_base);
echo $before_widget;
if ($title)
echo $before_title . $title . $after_title;
echo \'<div class="taglist">\';
//wp_tag_cloud( apply_filters(\'widget_tag_cloud_args\', array(\'taxonomy\' => $current_taxonomy) ) );
echo \'<ul>\';
$args = array(\'taxonomy\' => \'post_tag\');
$terms = get_terms(\'post_tag\', $args);
$count = count($terms);
$i = 0;
if ($count > 0) {
foreach ($terms as $term) {
$i++;
$term_list .= \'<li><a href="/tag/\' . $term->slug .
\'" title="View all post filed under \' . $term->name . \'">\' . $term->name .
\'</a></li>\';
}
echo $term_list;
}
echo \'</ul>\';
echo "</div>\\n";
echo $after_widget;
}
function update($new_instance, $old_instance)
{
$instance[\'title\'] = strip_tags(stripslashes($new_instance[\'title\']));
$instance[\'taxonomy\'] = stripslashes($new_instance[\'taxonomy\']);
return $instance;
}
function form( $instance ) {
$current_taxonomy = $this->_get_current_taxonomy($instance);
?>
<p><label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" value="<?php if (isset ( $instance[\'title\'])) {echo esc_attr( $instance[\'title\'] );} ?>" /></p>
<p><label for="<?php echo $this->get_field_id(\'taxonomy\'); ?>"><?php _e(\'Taxonomy:\') ?></label>
<select class="widefat" id="<?php echo $this->get_field_id(\'taxonomy\'); ?>" name="<?php echo $this->get_field_name(\'taxonomy\'); ?>">
<?php foreach ( get_object_taxonomies(\'post\') as $taxonomy ) :
$tax = get_taxonomy($taxonomy);
if ( !$tax->show_tagcloud || empty($tax->labels->name) )
continue;
?>
<option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
<?php endforeach; ?>
</select></p><?php
}
function _get_current_taxonomy($instance) {
if ( !empty($instance[\'taxonomy\']) && taxonomy_exists($instance[\'taxonomy\']) )
return $instance[\'taxonomy\'];
return \'post_tag\';
}
}
register_widget(\'WP_Widget_Tag_List\');